make fgets() wait for new lines.

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 35
Reputation: CoolAtt is an unknown quantity at this point 
Solved Threads: 0
CoolAtt's Avatar
CoolAtt CoolAtt is offline Offline
Light Poster

make fgets() wait for new lines.

 
0
  #1
Jan 20th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: make fgets() wait for new lines.

 
0
  #2
Jan 20th, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 35
Reputation: CoolAtt is an unknown quantity at this point 
Solved Threads: 0
CoolAtt's Avatar
CoolAtt CoolAtt is offline Offline
Light Poster

Re: make fgets() wait for new lines.

 
0
  #3
Jan 21st, 2009
i tried it. its not working.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: make fgets() wait for new lines.

 
0
  #4
Jan 21st, 2009
Oh, thanks for the update, be sure to add more information later.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 35
Reputation: CoolAtt is an unknown quantity at this point 
Solved Threads: 0
CoolAtt's Avatar
CoolAtt CoolAtt is offline Offline
Light Poster

Re: make fgets() wait for new lines.

 
0
  #5
Jan 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 90
Reputation: ajay.krish123 is an unknown quantity at this point 
Solved Threads: 8
ajay.krish123 ajay.krish123 is offline Offline
Junior Poster in Training

Re: make fgets() wait for new lines.

 
0
  #6
Jan 21st, 2009
Originally Posted by CoolAtt View Post
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 44
Reputation: me_ansh is an unknown quantity at this point 
Solved Threads: 5
me_ansh me_ansh is offline Offline
Light Poster

Re: make fgets() wait for new lines.

 
0
  #7
Jan 21st, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 35
Reputation: CoolAtt is an unknown quantity at this point 
Solved Threads: 0
CoolAtt's Avatar
CoolAtt CoolAtt is offline Offline
Light Poster

Re: make fgets() wait for new lines.

 
0
  #8
Jan 23rd, 2009
thx for the code.
but because there is an infinite loop cpu usage is 100%.
do you know how to prevent this ?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: sma245 is an unknown quantity at this point 
Solved Threads: 1
sma245 sma245 is offline Offline
Newbie Poster

Re: make fgets() wait for new lines.

 
0
  #9
Jan 23rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 44
Reputation: me_ansh is an unknown quantity at this point 
Solved Threads: 5
me_ansh me_ansh is offline Offline
Light Poster

Re: make fgets() wait for new lines.

 
0
  #10
Jan 25th, 2009
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...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC