I'm currently working with processes, and encountered a problem while reading and writing char to a file.
The idea is we have couple of processes which should read an integer from file, increment it and write back. Here is my attempt: (i wont include error checking)
...
char n;
char buff[5];
int number;
...
read(my_desc, &n, 1);
number = (int)n;
number ;
sprintf(buff, "M", number);
write(my_desc, buff, sizeof(buff));
...
The file is just plain
0
But the output seems to be not correct (almost always garbage).
I already read write and read manuals but im clueless. I've checked some topics on read and write functions here on stack overflow, but most of them either don't work for me or i struggle with implementation.
Thanks in advance.
CodePudding user response:
It appears that you are reading a single character, taking the ASCII code of that character and converting that number to a 4-character string, and then writing those 4 characters and the terminating null character back to the file.
According to the information that you provided in the comments section, this is not intended. If I understand you correctly, you rather want to
- read the entire file as a string,
- convert that string to a number,
- increment that number,
- convert that number back to a string and
- overwrite the entire file with that string.
Step #1 can be accomplished with the function read
. However, you should read in the whole file instead of only a single character.
Step #2 can be accomplished by using the function strtol
.
Step #3 is trivial.
Step #4 can be accomplished using the function snprintf
.
Step #5 can be accomplished by rewinding the file using the function lseek
, and then using the function write
.
I am assuming that the number represented in the file is in the respresentable range of a long int
, which is -9,223,372,036,854,775,808
to 9,223,372,036,854,775,807
on most POSIX platforms. This means that the length of the string can be up to 19 characters, 20 including the terminating null character. That is why I am using a buffer size of 20
.
char buffer[20], *p;
ssize_t bytes_read;
long num;
bytes_read = read( my_desc, buffer, (sizeof buffer) - 1 );
if ( bytes_read <= 0 )
{
//TODO: handle input error
}
//add null terminating character to string
buffer[bytes_read] = '\0';
//attempt to convert the string to a number
num = strtol( buffer, &p, 10 );
//check for conversion error
if ( p == buffer )
{
//TODO: handle conversion error
}
//increment the number
num ;
//write incremented number to buffer
snprintf( buffer, sizeof buffer, "%ld", num );
//rewind file
lseek( my_desc, 0, SEEK_SET );
//write buffer to file
write( my_desc, buffer, strlen(buffer) );
Note that I have not tested this code.
Also note that this program assumes that the input file does not contain any leading zeros. If the file contains the text string "003"
, then this program will overwrite the first character with a 4
, but leave the remaining characters in the file intact. If this is an issue, then you will have to add a call to ftruncate
to truncate the file.