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.

Recommended Answers

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
}
Jump to Post

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

Jump to Post

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 …
Jump to Post

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

Jump to Post

All 14 Replies

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
}

i tried it. its not working.

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

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

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

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

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;
}

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

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

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...

hi everyone.

i modified [B]me_ansh[/B] 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()

}

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

yes i have assigned it.

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

hi .

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.