Fadi Komati wrote:
Dear All,
I need your help concerning the following.
I am openeing a log file for processing (I need to look for certain
activities)
obviously i keep reading until the end of file.
What i need is the following: I need to be able to reload the file in
order to read the entries added to the end of the file. Obviouly, i
would like the file pointer to be positioned where i stopped reading .
can anyone help me with this ( a small code sample would be
marvelous). Or if you can tell me what function call to use in order
to reread the file and thus see the changes.
Please advise.
Regards
The following looks for a log file and opens or creates it.
It reads the file to the end, adds a new line and reads that.
You need not 'rebuffer' the file. The fseek() allows the change from
read to write to read again.
If you can read it, you can have it.
#include <stdio.h>
int main(void) {
FILE *log;
char *logfile = "fk.log";
char line[80];
int n = 0;
long pos;
log = fopen(logfile, "a+");
rewind(log);
while (fgets(line, sizeof line, log)) {
++n;
printf(line);
}
pos = ftell(log); /* actually end of file */
printf("There are %d lines in %s\n", n, logfile);
fseek(log, pos, SEEK_SET);
fprintf(log, "Log Line %3d\n", ++n);
fseek(log, pos, SEEK_SET);
fgets(line, sizeof line, log);
printf(line);
fclose(log);
return 0;
}
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---