Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > rebuffering an opened file

Reply
Thread Tools

rebuffering an opened file

 
 
Fadi Komati
Guest
Posts: n/a
 
      05-31-2004
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
 
Reply With Quote
 
 
 
 
lallous
Guest
Posts: n/a
 
      05-31-2004
Hello Fadi,

"Fadi Komati" <> wrote in message
news: om...
> 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


 
Reply With Quote
 
 
 
 
Vijay Kumar R Zanvar
Guest
Posts: n/a
 
      05-31-2004

"Fadi Komati" <> wrote in message news: om...
> 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.

--
"There's money in this case, Watson," he continued,
glancing out of the window, "if there is nothing else."
- A Scandal in Bohemia


 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      05-31-2004
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 private.php?do=newpm&u=
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
listdir reports [Error 1006] The volume for a file has been externally altered so that the opened file is no longer valid PerOK Python 2 01-08-2009 07:58 AM
Streaming file to user: 'File could not be opened' CJM ASP General 7 03-30-2006 10:38 AM
Videos and audio rebuffering Fred W Computer Support 2 01-23-2006 01:20 PM
Accidentaly opened I-Bagle - and then opened virus vault ?? Morph Computer Information 2 02-01-2005 03:43 AM
How do you attach a file to a hyperlink so that it cab be opened... =?Utf-8?B?Umlja0w=?= ASP .Net 1 05-28-2004 01:29 PM



Advertisments