943,805 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1812
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 20th, 2009
0

make fgets() wait for new lines.

Expand Post »
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:
  1. while(true){ //infinite loop
  2.  
  3. if(fgets(line,size,fp)!=NULL)
  4. process line.
  5.  
  6. }
plz help.
Last edited by Narue; Jan 20th, 2009 at 9:23 am. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008
Jan 20th, 2009
0

Re: make fgets() wait for new lines.

Something like this (untested)
  1. while ( 1 ) {
  2. while ( fgets(line,size,fp)!=NULL) {
  3. // process lines
  4. }
  5. clearerr( fp ); // reset end of file flag
  6. sleep( 1 ); // wait for more lines to arrive
  7. }
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 21st, 2009
0

Re: make fgets() wait for new lines.

i tried it. its not working.
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008
Jan 21st, 2009
0

Re: make fgets() wait for new lines.

Oh, thanks for the update, be sure to add more information later.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 21st, 2009
0

Re: make fgets() wait for new lines.

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

will clearerr( fp ) tell fgets() that EOF has not been reached?
Last edited by CoolAtt; Jan 21st, 2009 at 3:18 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008
Jan 21st, 2009
0

Re: make fgets() wait for new lines.

Click to Expand / Collapse  Quote originally posted by CoolAtt ...
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
Reputation Points: 6
Solved Threads: 9
Junior Poster in Training
ajay.krish123 is offline Offline
90 posts
since Nov 2008
Jan 21st, 2009
0

Re: make fgets() wait for new lines.

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

  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int main(void)
  5. {
  6. char buff[100];
  7. FILE* fp_1;
  8.  
  9. fp_1 = fopen("./test.txt","r");
  10. memset(buff,0,sizeof(buff));
  11.  
  12. while(1)
  13. {
  14. if(fgets(buff,100,fp_1))
  15. ;
  16. else
  17. {
  18. printf("\n %s \n",buff); //process the last appended line here
  19. fclose(fp_1);
  20. fp_1 = fopen("./test.txt","r");
  21. }
  22.  
  23. }
  24.  
  25. return 0;
  26. }
Reputation Points: 13
Solved Threads: 5
Light Poster
me_ansh is offline Offline
44 posts
since Jan 2009
Jan 23rd, 2009
0

Re: make fgets() wait for new lines.

thx for the code.
but because there is an infinite loop cpu usage is 100%.
do you know how to prevent this ?
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008
Jan 23rd, 2009
0

Re: make fgets() wait for new lines.

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sma245 is offline Offline
2 posts
since Jan 2009
Jan 25th, 2009
0

Re: make fgets() wait for new lines.

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...
Reputation Points: 13
Solved Threads: 5
Light Poster
me_ansh is offline Offline
44 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Prime numbers
Next Thread in C Forum Timeline: pointer to member of struct





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC