I am reading a growing input file, and do some work and write the info to the output file. I have some conditions to work on the growing file. But I am failing at exiting from the loop.
FILE *logfile;
int main(int argc, char *argv[])
{
char *filename;
char *logfilename;
FILE *infile;
char line_buf[255];
char *line;
sleep(3);
if (argc < 3) {
fprintf(stderr, "Usage: %s <filename> <logfile>\n",
argv[0]);
return -1;
}
filename = argv[1];
logfilename = argv[2];
infile = fopen(filename, "rb");
if (infile == NULL) {
fprintf(stderr, "Failed to open file\n");
return -1;
}
logfile = fopen(logfilename, "w");
if (logfile == NULL) {
fprintf(stderr, "Failed to open logfile - are permissions correct?\n");
return -1;
}
while(1){
line = fgets(line_buf, sizeof(line_buf), infile);
if (line == NULL){
if(feof(infile))
clearerr(infile);
failedReads++;
usleep(25000); //wait for the data from live file
continue;
}
else{
if(feof(infile))
break;
}
...........
//do some work
...........
}
fclose(infile);
fclose(logfile);
}
My output log file is getting the data only after the input file stops growing(means at the end of execution). I want my output logfile to get the data by time(means output file is not growing). I have a python script for create a growing file(If anyone really wants to work my issue).
#/usr/bin/python
import time
with open("input.txt") as f:
fileoutput = f.readlines()
with open("out.txt", "a+") as f1:
for line in fileoutput:
f1.write(line)
f1.flush()
time.sleep(0.01)