473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

rebuffering an opened file

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
Nov 14 '05 #1
3 1317
Hello Fadi,

"Fadi Komati" <fa*********@act.com.lb> wrote in message
news:81**************************@posting.google.c 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
Nov 14 '05 #2

"Fadi Komati" <fa*********@act.com.lb> wrote in message news:81**************************@posting.google.c 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
Nov 14 '05 #3
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 ---
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Douglas Buchanan | last post by:
Buttons don't work if form is opened on startup A2k If 'frmMain' is set to open by default at startup none of the buttons work. If 'frmMain' is opened from the database window then all the...
7
by: emanshu | last post by:
HI all, I an designing an application in C++. i want to open file requested by end user but i want to reflect an error to user if file is already opened by some other application.. will anybody...
10
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I want to create an event that is fiered when a file is accessed (Opened). What i want to do is monitor a directory and subdirectory if files are opened. I have tried to use the...
1
by: Alexander Korsunsky | last post by:
Hi! Is it possible to extract the mode flags of a stream (FILE* stream or fstream - object), without looking at how the stream was opened? What I mean by mode flags is wether the stream is opened...
13
by: thomas.mertes | last post by:
Hello Recently I discovered some problem. I have some C code which determines how many bytes are available in a file. Later I use this information to malloc a buffer of the correct size before...
9
by: tshad | last post by:
I am trying to get access to a file that may still being written because the file is so large (7-10MB). I get an error: The process cannot access the file 'c:\TestDocs\XMLFiles\492172.XML'...
2
by: BAnderton | last post by:
Greetings from beautiful Tucson, Arizona. Question: Is there a way in Python to determine what all file identifiers have been opened by Python, and to close them all? Why I ask: I learned...
36
by: Don | last post by:
I wrote an app that alerts a user who attempts to open a file that the file is currently in use. It works fine except when the file is opened by Notepad. If a text file is opened, most computers...
15
by: =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all, For some reasons, somewhere in a program, I'd like, if possible, to quickly parse a whole file before rewinding it and letting the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.