473,772 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1342
Hello Fadi,

"Fadi Komati" <fa*********@ac t.com.lb> wrote in message
news:81******** *************** ***@posting.goo gle.com...
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*********@ac t.com.lb> wrote in message news:81******** *************** ***@posting.goo gle.com...
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
3050
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 buttons work. The form's name ('frmMain') is selected from in the Startup dialog box.
7
14005
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 tell me that how to know that the file is already open or closed.. any help is highly appreciated...
10
5615
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 FileSystemWatcher. But it didn't work as i expected. I cant get it to fire an event when a file is accessed.
1
3852
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 in read only mode, write only, binary and so on. I did not find any functions in C, but I found something similar in C++, the flags() member functio of an fstream object. I've tried to check the flags on ios_base::binary ios_base::in binary...
13
4498
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 I read the bytes. Determining the number of bytes available in a file is done in 5 steps: 1. Use tell(aFile) to get the current position.
9
5416
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' because it is being used by another process
2
10893
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 Python after cutting my programming teeth on Matlab, where you get a list of all open file identifiers (that is, those opened from a particular Matlab session) with "fopen('all')" and close them with "fclose('all')". In my 4 years of experience...
36
5404
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 are configured to use Notepad to open the file by default and if they are configured to use Notepad by default I want it to remain that way rather than retrieve the text into my app or force the user to use another app to read the file. I'm...
15
3187
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 full analysis start. My problem is that the FILE* I want do parse has been fopen'ed far away from where I am and I don't know in which MODE my FILE* has been opened. And additionally, my FILE* may not be a regular file, but a continuous
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.