I write C code to read data from a file (and then process it), which was copied by some software for 9 seconds. Here is a prototype of the real code (I did not paste the original code here, 250 lines) -
while(1){ while(ch ! = EOF){ ch = fgetc(File pointer) //read from the file and do the required } clearerr(File pointer); sleep(9); //after every 9 seconds the new data is added to the file by some external software ch = fgetc(File pointer); printf("The value of ch is %d",ch); }
But here the code reads the data from the data file once, then when the EOF happens, it exits the inner while loop and goes into the outer loop and gets stuck there only. I expected clearerr (File pointer) to remove EOF from the file so that I can read recently updated data, but instaed, I am stuck in this EOF and cannot read new data. The print statement continues to print EOF, so it means that it is not deleted from the file. How can I fix this problem?
Since I did not have much knowledge about this other software, I tried updating the data myself, inserting and saving 1 EOF after detection, and my program is in sleep mode
Also, when I ran my program in one terminal and tail -f file_name.txt in another terminal parallel to the program, everything worked fine. I can read new data. Why is this so?
Note. . I already read this question (link below), and accordingly I used clearerr (file pointer), but did not delete the current EOF, so I can not read recently updated data from the file.
Reading from a file that is constantly being updated
Here is the source code -
#include<stdio.h>
source share