954,153 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

make fgets() wait for new lines.

Hi.
am using fgets() to read lines from a text file.
the file is being updated after a few seconds.

i want to read and process the last appended line at the end of the file.

i tried the following:

while(true){ //infinite loop

if(fgets(line,size,fp)!=NULL)
    process line.

}

plz help.

CoolAtt
Light Poster
39 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Something like this (untested)

while ( 1 ) {
  while ( fgets(line,size,fp)!=NULL) {
    // process lines
  }
  clearerr( fp );  // reset end of file flag
  sleep( 1 );  // wait for more lines to arrive
}
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

i tried it. its not working.

CoolAtt
Light Poster
39 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Oh, thanks for the update, be sure to add more information later.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

does fgets() gets aware when a file is updated ?

will clearerr( fp ) tell fgets() that EOF has not been reached?

CoolAtt
Light Poster
39 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

does fgets() gets aware when a file is updated ?

will clearerr( fp ) tell fgets() that EOF has not been reached?


If the above method is not working then you must go for closing and reopening of the file when it gets updated

ajay.krish123
Junior Poster in Training
90 posts since Nov 2008
Reputation Points: 6
Solved Threads: 9
 

Okay....I think, the following prototype might help you...
- I made this on Linux and compiled with gcc
- 'test.txt' was in the same directory as my '.c' file

#include<stdio.h>
#include<string.h>

int main(void)
{
    char buff[100];
    FILE* fp_1;

    fp_1 = fopen("./test.txt","r");
    memset(buff,0,sizeof(buff));

    while(1)
    {
        if(fgets(buff,100,fp_1))
              ;
        else
        {
            printf("\n %s \n",buff);  //process the last appended line here
            fclose(fp_1);
            fp_1 = fopen("./test.txt","r");
        }

    }

    return 0;
}
me_ansh
Light Poster
44 posts since Jan 2009
Reputation Points: 13
Solved Threads: 5
 

thx for the code.
but because there is an infinite loop cpu usage is 100%.
do you know how to prevent this ?

CoolAtt
Light Poster
39 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

You could try using sleep(n) in the loop body. This will make your code defer to some other thread for at least n milliseconds, and free up the CPU. Note: is _sleep() under Win32

sma245
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 1
 

You may check out that are there any kind of signals that are generated and can be handled when a text file gets updated....if yes, then you can use signal handling and make your program wait until a signal is received and then process the data in file....that will definitely reduce your CPU usage...

me_ansh
Light Poster
44 posts since Jan 2009
Reputation Points: 13
Solved Threads: 5
 

hi everyone.

i modified me_ansh code and its working.
The problem is that when it reaches the last line the program crashes
(segmentation fault)

Plz help.
thx

My implementation:

while(1){

while(!feof(fp)){

if(fgets(line,1000,fp))
//process line
}

fclose()
fopen()

}

CoolAtt
Light Poster
39 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

And did you assign the result of fopen ?
And please learn how to use code tags.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

yes i have assigned it.

CoolAtt
Light Poster
39 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Well I guess you'll just have to post your real code, and not just random bits of pseudo-code.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

hi .

i managed to find the bug. now it's not crashing.
thanks.
Btw i will use use code tags next time :)

CoolAtt
Light Poster
39 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You