| | |
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 ansi api array arrays asterisks bash binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql number open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming stack standard strchr string strings structures suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






