Reading data from a file, which is often updated in C

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> #include<stdlib.h> #include"link.h" #include<string.h> #include<math.h> #include<unistd.h> char file_time[20],file_date[20],file_year[20],file_month[20],time_interval[20],mac_addr[40]; float store_voltage,store_pressure,store_temp1,store_temp2,store_humid,store_ntctherm,rssi_no,lqi_no; char input_filename[30],output_filename[30],corrected_year[20],ch; int first_time =0,cursor_position=0,no_of_stack,timestamp_detected=0,file_has_ended=0; stack *s; int input_func,flag=1,order=1,restart=0; void output(){ FILE *fi = fopen(output_filename,"a"); /*file is opened and closed after each input by the user because in the output user only wants the data for last some of the readings for plotting the graph*/ printf("How many stacks you want to print\n"); while(1){ if(scanf("%d",&no_of_stack)!=1){ //error handling printf("Please enter valid input\n"); scanf("%c",&ch); } else break; } /*like the program keep on recording the data as stacks and prints whatever no of stacks i want it to print , also this asking od the no_of_stacks should take place till the program runs*/ printList(&s,no_of_stack,fi); printf("Please wait, Updating logs..............................................................\n"); fclose(fi); // sleep(atoi(time_interval)); //this sleep can be removed also, this is done so as to // sleep(2); printf("Done \n"); } void input(){ int i=0,j,initial_lines=0; char ch=0; fflush(stdout); FILE *fp = fopen(input_filename,"r"); /*file is closed and opened in each iteration to handle buffer overflow because since the data in input file keeps on increasing so the input buffer will also increase so to tackle this the file is reopened again and again and the file pointer starts reading from the point where it left off instead of reading whole file again and again*/ if(fp==NULL){ printf("input_filename - %s",input_filename); printf("No file found of this name: %s\n",input_filename); exit(1); } ch=fgetc(fp); ch = fgetc(fp); if(first_time == 1){ printf("file is openend first time\n"); //this is to record whether it the first time user reading the file while(1){ if(initial_lines == 3){ break; // This is just to skip some useless lines in input file } if(ch=='\n'){ initial_lines++; } ch = fgetc(fp); } initial_lines = 0; first_time =0; } fseek(fp, 10, SEEK_CUR); printf("Cursor set to %d position after fseek\n",cursor_position); ch=fgetc(fp); if(ch==EOF){ printf("here comes ch %d\n",ch);} char str[100]; while(1){ while(ch!=EOF){ //reading till end of file input_func++; timestamp_detected=0; restart=0; fscanf(fp,"%s",str); while(1){ //this is just string matching for taking input if(!strcmp(str,"Timestamp")){ timestamp_detected=1; break; } if(fscanf(fp,"%s",str)==EOF){ file_has_ended = 1; break; } } if(timestamp_detected){ //taking input fscanf(fp,"%s",str); fscanf(fp,"%s",file_month); fscanf(fp,"%s",file_date); fscanf(fp,"%s",file_time); fscanf(fp,"%s",file_year); fscanf(fp,"%s",str); // printf("%s %s") for(i=0;i<4;i++){ corrected_year[i] = file_year[i]; } corrected_year[4] = '\0'; fseek(fp,36,SEEK_CUR); fscanf(fp,"%s",mac_addr); // printf("mac addr - %s",mac_addr); fscanf(fp,"%s",str); if(!strcmp(str,"RSSI")){ fscanf(fp,"%f",&rssi_no); } else restart =1; fscanf(fp,"%s",str); fscanf(fp,"%s",str); fscanf(fp,"%s",str); if(!strcmp(str,"LQI")){ fscanf(fp,"%f",&lqi_no); } else restart =1; if(restart){ continue; } order=1; while(fscanf(fp,"%s",str)){ if(!strcmp(str,"+[Node_Voltage]")){ fseek(fp,3,SEEK_CUR); fscanf(fp,"%f",&store_voltage); order=2; break; } else if(!strcmp(str,"Timestamp")){ fseek(fp,-20,SEEK_CUR); restart =1; break; } } if(restart){ continue; } fscanf(fp,"%s",str); printf("str value - %s\n",str); while(fscanf(fp,"%s",str)){ if( !strcmp(str,"+[P_MS5637]") && order==2 ){ fseek(fp,7,SEEK_CUR); fscanf(fp,"%f",&store_pressure); printf("pressure - %f stored, order - %d\n",store_pressure,order); order++; } fscanf(fp,"%s",str); fscanf(fp,"%s",str); if( !strcmp(str,"+[NTC_THERM") && order==3 ){ fscanf(fp,"%s",str); fscanf(fp,"%s",str); fseek(fp,5,SEEK_CUR); fscanf(fp,"%f",&store_ntctherm); printf("ntc_therm - %f stored, order - %d\n",store_ntctherm,order); order++; } fscanf(fp,"%s",str); fscanf(fp,"%s",str); fscanf(fp,"%s",str); printf("after ntc%s\n",str); if( !strcmp(str,"+[Temp_LM75B]") && order==4 ){ fseek(fp,5,SEEK_CUR); fscanf(fp,"%f",&store_temp1); printf("temprature - %f stored, order - %d\n",store_temp1,order); order++; } fscanf(fp,"%s",str); fscanf(fp,"%s",str); fscanf(fp,"%s",str); if( !strcmp(str,"+[RH_CC2D33S]") && order==5 ){ fseek(fp,5,SEEK_CUR); fscanf(fp,"%f",&store_humid); printf("humid - %f stored, order - %d\n",store_humid,order); order++; } fscanf(fp,"%s",str); fscanf(fp,"%s",str); if( !strcmp(str,"+[Temp_CC2D33S]") && order==6){ fseek(fp,3,SEEK_CUR); fscanf(fp,"%f",&store_temp2); printf("temp2 - %f stored, order - %d\n",store_temp2,order); order++; } if(order==7){ printf("items pushed\n\n\n"); push(&s,file_date,file_time,file_month,corrected_year,store_pressure,store_ntctherm,store_temp1,store_temp2,store_humid,store_voltage,rssi_no,lqi_no,mac_addr); break; } else{ break; } } } ch=fgetc(fp); if(ch==EOF) printf("the value of EOF is %d\n",ch); cursor_position = ftell(fp); //recording the cursor position } printf("\n\n\nend of file detected\n"); clearerr(fp); sleep(9); printf("i'm sleeping\n"); fscanf(fp,"%s",str); printf("the value of str is after sleeping- %s\n",str); ch = fgetc(fp); printf("the value of ch is after sleeping- %d\n",ch); } // fclose(fp); } //shift the control of the program, printing takes place........... int main(int argc, char *argv[]){ int input_func = 0; // Enter the time interval in seconds if(argv[1]==NULL || argv[2]==NULL || argv[3]==NULL){ printf("Invalid Format, Use the format specified below\n ./[executable] [Input File Name] [Output File Name] [Time Interval(in seconds)]\n"); exit(1); } strcpy(time_interval,argv[3]); strcpy(output_filename,argv[2]); strcpy(input_filename,argv[1]); create(&s); //creating the stack first_time = 1; while(1){ input(); //runin the input function then output(); } return 0; } 
0
source share

Source: https://habr.com/ru/post/1482483/


All Articles