| | |
make fgets() wait for new lines.
Thread Solved |
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:
plz help.
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:
c Syntax (Toggle Plain Text)
while(true){ //infinite loop if(fgets(line,size,fp)!=NULL) process line. }
Last edited by Narue; Jan 20th, 2009 at 9:23 am. Reason: added code tags
Something like this (untested)
C Syntax (Toggle Plain Text)
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 }
•
•
Join Date: Jan 2009
Posts: 44
Reputation:
Solved Threads: 5
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
- I made this on Linux and compiled with gcc
- 'test.txt' was in the same directory as my '.c' file
C Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Jan 2009
Posts: 44
Reputation:
Solved Threads: 5
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...
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Prime numbers
- Next Thread: pointer to member of struct
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






