从C语言中频繁更新的文件中读取数据

从C语言中频繁更新的文件中读取数据,c,file-io,C,File Io,我正在编写一个C代码,从一个文件中读取数据(然后对其进行处理),该文件由一些软件以9秒的间隔不断追加 这是实际代码的原型(我没有在这里粘贴原始代码,它有250行)—— 但在这里,代码从数据文件中读取数据一次,然后当EOF发生时,它从内部while循环中断,进入外部循环,只在那里卡住。我希望clearerr(文件指针)将从文件中删除EOF,以便我能够读取最新更新的数据,但我发现我被该EOF卡住了,无法读取新数据。print语句继续打印EOF,因此它意味着它不会从文件中删除。 如何解决此问题 因为我

我正在编写一个C代码,从一个文件中读取数据(然后对其进行处理),该文件由一些软件以9秒的间隔不断追加 这是实际代码的原型(我没有在这里粘贴原始代码,它有250行)——

但在这里,代码从数据文件中读取数据一次,然后当EOF发生时,它从内部while循环中断,进入外部循环,只在那里卡住。我希望clearerr(文件指针)将从文件中删除EOF,以便我能够读取最新更新的数据,但我发现我被该EOF卡住了,无法读取新数据。print语句继续打印EOF,因此它意味着它不会从文件中删除。 如何解决此问题

因为我对其他软件不太了解,所以我尝试自己更新数据,在检测到1个EOF并且我的程序处于睡眠状态后粘贴并保存数据

另外,当我在一个终端上运行程序,并在另一个终端上并行运行
tail-f file_name.txt
时,程序运行正常。我能读懂新的数据。为什么会这样

注意-我已经阅读了这个问题(下面给出的链接),因此我使用了clearerr(文件指针),但它没有删除当前EOF,因此我无法从文件中读取最新更新的数据

这是原始代码-

#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's the first time user's 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; 

}
#包括
#包括
#包括“link.h”
#包括
#包括
#包括
字符文件时间[20],文件日期[20],文件年份[20],文件月份[20],时间间隔[20],mac地址[40];
浮子存储电压、存储压力、存储温度1、存储温度2、存储湿度、存储温度、rssi编号、lqi编号;
字符输入文件名[30],输出文件名[30],更正年份[20],ch;
int first_time=0,光标_位置=0,_堆栈无_,检测到的时间戳_=0,文件_已_结束=0;
堆栈*s;
int input_func,标志=1,顺序=1,重新启动=0;
无效输出(){
FILE*fi=fopen(output_filename,“a”);/*文件在用户每次输入后打开和关闭,因为
输出用户只需要用于绘制图形的最后一些读数的数据*/
printf(“要打印的堆栈数量\n”);
而(1){
if(scanf(“%d”,&no_of_stack)!=1){//错误处理
printf(“请输入有效的输入\n”);
scanf(“%c”和“ch”);
}
其他的
打破
}          
/*像程序一样,继续将数据记录为堆栈和打印
无论我希望打印多少个堆栈,也要在
程序运行*/
打印列表(&s,堆栈的编号,fi);
printf(“请稍候,正在更新日志…”\n);
fclose(fi);
//sleep(atoi(time_interval));//也可以删除此睡眠,这样做是为了
//睡眠(2);
printf(“完成”\n);
}
无效输入(){
int i=0,j,初始_线=0;
char ch=0;
fflush(stdout);
FILE*fp=fopen(input_filename,“r”);/*文件在每次迭代中关闭和打开,以处理缓冲区溢出
因为由于输入文件中的数据不断增加,所以输入缓冲区也会增加,所以为了解决这个问题,文件是
一次又一次地重新打开,文件指针从它停止的点开始读取,而不是读取整个文件
一次又一次*/
如果(fp==NULL){
printf(“输入文件名-%s”,输入文件名);
printf(“未找到此名称的文件:%s\n”,输入\u文件名);
出口(1);
}
ch=fgetc(fp);
ch=fgetc(fp);
如果(第一次==1){
printf(“file is openend first time\n”);//用于记录用户是否第一次读取该文件
而(1){
如果(初始_行==3){
break;//这只是为了跳过输入文件中一些无用的行
}
如果(ch='\n'){
初始_行++;
}
ch=fgetc(fp);
}
初始_线=0;
第一时间=0;
} 
fseek(fp,10,SEEK_CUR);
printf(“光标在fseek\n之后设置为%d位置”,光标位置);
ch=fgetc(fp);
如果(ch==EOF){
printf(“ch%d\n,ch);}
char-str[100];
而(1){
而(ch!=EOF){//一直读到文件末尾
输入函数++;
检测到的时间戳_=0;
重新启动=0;
fscanf(fp,“%s”,str);
而(1){//这只是获取输入的字符串匹配
如果(!strcmp(str,“时间戳”)){
检测到的时间戳_=1;
打破
}
如果(fscanf(fp,“%s”,str)=EOF){
文件\u已\u结束=1;
打破
}
}
如果(检测到时间戳){//正在进行输入
fscanf(fp,“%s”,str);
fscanf(fp,“%s”,每月存档);
fscanf(fp,“%s”,文件日期);
fscanf(fp,“%s”,文件时间);
fscanf(fp,“%s”,文件年份);
fscanf(fp,“%s”,str);
//printf(“%s%s”)

对于(i=0;我不明白为什么你链接的问题的答案不能解决你的问题。没有a,我投票结束你的问题。在我在问题中给出的链接中,请看JonathanLeffler的评论,我在这里实现了这个东西,但它不起作用,但在其他终端中使用tail-f filename.txt它起作用。
ch的类型是什么
?如果是我的代码,内部while将是
while((ch=fgetc(fp))!=EOF){…}
,这将是
fgetc()的唯一用法
。如前所述,睡眠后读取的第一个字符将被丢弃。最好显示可编译的代码;
文件指针当然不是有效的标识符。MCVE()将
#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's the first time user's 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; 

}