I am trying to write a fixed number of random integers into a file, using the system call "write", and then check if they are written properly. In order to do this, I call the "read" function, then I read all the file content, integer by integer and I print them into another file.
The problem is that although the write function writes properly the random integers (500 in size), the read function is able to extract e.g 200 of them and then it returns garbage for the rest 300. Sometimes, it returns only 10 of them. Every time I run the code, it has a different behavior.
I have checked that the "write" function writes all the numbers correctly by printing them to another file (e.g file1). But, printing the numbers that the "read" function returns (e.g. file2) showed me that they are not all the same.
I realized that if I use a single number (e.g. 5) instead of a random integer, it all works fine. I googled a lot, I checked if I open/close the files properly and I could not solve the problem.
Here is my code in C. I removed some error checks in order to show you a small script.
Can you please help me?
int main()
{
srand ( time(NULL) );
// write
int fd = open("file_test_1", O_CREAT | O_WRONLY);
int val;
if (fd != -1) {
for(int i=0;i<500;i++){
val = (int) rand()%500;
// val = 5;
write(fd, &val, sizeof(val));
}
}
close(fd);
FILE * file_check;
file_check=fopen("file_test_1_check","w");
// test read
fd = open("file_test_1", O_RDONLY);
int new_val;
int x;
if (fd != -1) {
while( ( x = read(fd, &new_val, sizeof(new_val)) )>0 ){
fprintf(file_check,"%d\t\t%d\n",new_val, x);
}
}
close(fd);
fclose(file_check);
return 0;
}