C++ .net, reading an opened file.

P

pboulay

Hi,

I want to do like the command "tail -f" on unix.
I want to read a LOG file and catch all new entries into that file.

Someone have any idea?

Thanks
Patrick
 
V

Victor Bazarov

I want to do like the command "tail -f" on unix.
I want to read a LOG file and catch all new entries into that file.

Someone have any idea?

There are several ways to read files in C++. Have you tried any of
them? You have 'FILE*' and the corresponding 'fread' or 'fgets',
you have 'std::ifstream' and the corresponding 'read' or 'getline'.

What C++ book are you reading that doesn't explain file I/O?

V
 
R

Richard Herring

Victor Bazarov said:
There are several ways to read files in C++. Have you tried any of
them? You have 'FILE*' and the corresponding 'fread' or 'fgets',
you have 'std::ifstream' and the corresponding 'read' or 'getline'.

What C++ book are you reading that doesn't explain file I/O?

I haven't met many C++ books that explain how to wait until a file is
changed by another application, then output the changes, which is
effectively what 'tail' does.

Short answer to the OP: you can't do it with standard C++.
 
V

Victor Bazarov

Richard Herring said:
I haven't met many C++ books that explain how to wait until a file is
changed by another application, then output the changes, which is
effectively what 'tail' does.

Short answer to the OP: you can't do it with standard C++.

Who says that one has to wait until "a file is changed by another
application"? Why not just do it periodically? *THAT* can be done
with standard C++, can't it?

V
 
R

Richard Herring

Victor Bazarov said:
Who says that one has to wait until "a file is changed by another
application"? Why not just do it periodically? *THAT* can be done
with standard C++, can't it?
For a small enough period, or if the other application doesn't mind you
hogging all the CPU time, maybe. I'm open to correction, but I don't
think it's going to be an elegant solution.
 
J

Jeff Schwab

Victor said:
Who says that one has to wait until "a file is changed by another
application"? Why not just do it periodically? *THAT* can be done
with standard C++, can't it?

V

In fact, that's exactly what tail -f does. The tail command doesn't get
notified magically when a file changes; rather, tail examines the file
every second or so to look for changes.

I believe some systems (Windows?) can notify processes of changes to
files by way of signals or call-back functions, but one would have to
use a platform-specific API to write such a program.
 
P

pboulay

Oh ok... if I check every second... it's can be ok.. i'll try it.. This
way will be easiest.

Thanks
 
I

Ioannis Vranos

Oh ok... if I check every second... it's can be ok.. i'll try it.. This
way will be easiest.

Since you are mentioning ".net" in the title, if you can use .NET, you
can catch various file system events including this.

Of course, this means that you will make use of system-dependent
facilities. But this is more in the area of it.
 
P

pboulay

I have a problem....

When I'm trying to open the log file, I got an exception with a message
who said that the file it's already opened by an other application.

Im reading the file in read only mode like this:
StreamReader *sr = new StreamReader(File::Open(m_File,
FileMode::Open, FileAccess::Read, FileShare::None));

How can I open a file opened by an other program?

Thanks
 
V

Victor Bazarov

I have a problem....

When I'm trying to open the log file, I got an exception with a message
who said that the file it's already opened by an other application.

Im reading the file in read only mode like this:
StreamReader *sr = new StreamReader(File::Open(m_File,
FileMode::Open, FileAccess::Read, FileShare::None));

How can I open a file opened by an other program?

It is definitely not part of C++, but your 'FileShare::None' probably
means that you are opening your file for _exclusive_ access. You need
to read more about 'File::Open' (whatever that is) and see what other
methods of "sharing" are available and more suitable for you. We here
cannot help you since it is undoubtedly a feature of your OS.

V
 
P

pboulay

I found the solution, the FileShare must be in ReadWrite... I dont
understand why, but now I can open the file.

Patrick
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top