473,395 Members | 1,762 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,395 software developers and data experts.

C++ .net, reading an opened file.

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

Jul 23 '05 #1
10 1851
<pb*****@hotmail.com> wrote...
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
Jul 23 '05 #2
In message <tf********************@comcast.com>, Victor Bazarov
<v.********@comAcast.net> writes
<pb*****@hotmail.com> wrote...
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?


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++.
--
Richard Herring
Jul 23 '05 #3
"Richard Herring" <ju**@[127.0.0.1]> wrote...
In message <tf********************@comcast.com>, Victor Bazarov
<v.********@comAcast.net> writes
<pb*****@hotmail.com> wrote...
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?


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
Jul 23 '05 #4
Victor Bazarov wrote:
"Richard Herring" <ju**@[127.0.0.1]> wrote...
In message <tf********************@comcast.com>, Victor Bazarov
<v.********@comAcast.net> writes
<pb*****@hotmail.com> wrote...

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?


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


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.
Jul 23 '05 #5
Oh ok... if I check every second... it's can be ok.. i'll try it.. This
way will be easiest.

Thanks

Jul 23 '05 #6
pb*****@hotmail.com wrote:
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.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #7
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

Jul 23 '05 #8
<pb*****@hotmail.com> wrote...
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
Jul 23 '05 #9
In message <Gd********************@comcast.com>, Victor Bazarov
<v.********@comAcast.net> writes
"Richard Herring" <ju**@[127.0.0.1]> wrote...
In message <tf********************@comcast.com>, Victor Bazarov
<v.********@comAcast.net> writes
<pb*****@hotmail.com> wrote...
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?


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?

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.

--
Richard Herring
Jul 23 '05 #10
I found the solution, the FileShare must be in ReadWrite... I dont
understand why, but now I can open the file.

Patrick

Jul 23 '05 #11

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

Similar topics

2
by: opt_inf_env | last post by:
Hello, I would like to solve the following problem. On the server side I have a file with a sequence of natural numbers (1, 2, 3, 4, 5, ...., n). Each user, after some action, adds new number...
3
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be...
0
by: Jim dunn | last post by:
HI I am having problems with C# with regards to its compatibility with win32 API methods, I am trying to read from a windows CE comm port using C# and imported methods from coredll.dll, it seems...
29
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and...
2
by: GeoUK | last post by:
Hi All, New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have. As part of a course I am doing at University I had...
10
by: lancer6238 | last post by:
Hi all, I'm having programs reading from files. I have a text file "files.txt" that contains the names of the files to be opened, i.e. the contents of files.txt are Homo_sapiens.fa...
3
by: rsk | last post by:
Hi Friends, I have the following code which reads the hexadecimal data from the "data.txt" file into the arrays. But when i run this code it is reading some garbage data after reading all the...
23
by: lisp9000 | last post by:
I wrote a small test program to read a file of data and print each line, but it's only printing the 2nd line out of 3 total lines. The test file, "foo.txt", has 3 lines: 7388: Zn->Z0 Run...
9
by: Amkcoder | last post by:
I am trying to read a file a character at a time, I want to read in the new line character. I am printing out each character to the screen. For some reason it will not print out the new line. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.