我正在编写一个C代码来读取文件中的数据(然后处理它),并在9秒的时间间隔内被某些软件强制附加 这是实际代码的原型(我这里没有粘贴原始代码,它是250行) -
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);
}
但是这里代码从数据文件中读取数据,然后当EOF发生时,它会突破内部while循环并转到外部循环并仅在那里卡住。我希望更清晰的(文件指针)将从文件中删除EOF,这样我就能够读取新更新的数据,但是我已经卡住了那个EOF并且无法读取新数据。 print语句继续打印EOF,这意味着它不会从文件中删除。 我该如何解决这个问题?
由于我对其他软件没有太多了解,我尝试自己更新数据,在检测到1个EOF并且我的程序处于睡眠状态后粘贴和保存
此外,当我在一个终端中运行我的程序而在另一个终端中tail -f file_name.txt
平行运行该程序时,一切正常。我能够读取新数据。为什么会这样?
注意 - 我已经阅读过这个问题(下面给出的链接),因此我使用了更清晰的(文件指针),但它没有删除当前的EOF,所以我无法阅读来自文件的新更新数据。
Read from a file that is continuously being updated
这是原始代码 -
#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;
}