| | |
make fgets() wait for new lines.
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
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 append array arrays bash binarysearch centimeter char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lowest match matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user whythiscodecausesegmentationfault win32api windows.h






