| | |
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
Views: 1247 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue mysql number oddnumber odf opensource overwrite owf pdf performance pointer pointers posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






