rebuffering an opened file

F

Fadi Komati

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
 
L

lallous

Hello Fadi,

Fadi Komati said:
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.

You can for instance use an additional file to store the last file position
(seek position) you used, and then when you want to open the log file again,
read the file position from that additional file and directly seek to that
position and continue reading.

Hope that helps,
Elias
 
V

Vijay Kumar R Zanvar

Fadi Komati said:
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

You want to:
+ Read the entire file when you open it for the first time
+ Read only that part which has not been read the last time, i.e.,
if the file was modified

If it is so, when you finish reading the file for the first time, you
can save a pointer to the position of your last read as follows:

long int pos;
FILE *fp = fopen ( ... );

/* read the file */

pos = ftell ( fp );
fclose ( fp );

Now, in the second reading you can do like this:

FILE *fp = fopen ( ... );
fseek ( fp, pos, SEEK_SET ); /* Now, use `pos' to jump */

Now start reading the file as file-pointer points where the newest data
were added. However, if the file size shrinks due to some reason this will
fail.

PS: There's no error checking done here. Just an overview.
 
J

Joe Wright

Fadi said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top