fstream data from file, with file periodically written with new data

R

roughtrader

Hi all,

I have a file that is written on a periodic basis. For example, the
number "1" with newline is written. 1 minute later, the number "2"
with newline is written. Another minute later, the number "3" is
written with newline, and so on and so on.

The resulting file looks like:

1
2
3
4
5
6
7
8
..
..
..


if I create an ifstream object, what code can I write to sense that
the file has changed with a new write, and subsequently, stream the
new data to a literal in a C++ program.

thanks for the help!

rt
 
R

Ramaswamy

usually for these types of application you need to open the file in
append mode.

You can do the following thing.
write the data to the file. find the position to where you have
witten the data.
next time before writing the data, get the position where next write
will place the data.
compare the positions, you will come to know, if any modification

Ramaswamy B.A
 
J

James Kanze

I have a file that is written on a periodic basis. For
example, the number "1" with newline is written. 1 minute
later, the number "2" with newline is written. Another minute
later, the number "3" is written with newline, and so on and
so on.
The resulting file looks like:

if I create an ifstream object, what code can I write to sense
that the file has changed with a new write, and subsequently,
stream the new data to a literal in a C++ program.

The simple answer is no. It's possible that with some
implementations, if you clear the error status once you've seen
end of file, and more data is present, you can read further, but
for various reasons this is generally considered a defect (at
least under Unix).

What you can do, however, is wrap your system level requests in
a custom streambuf, and initialize an istream with this. After
that, it's up to you to define the behavior of the streambuf
once it has returned EOF. In this case, to be really sure I
didn't get any anomalous behavior, I'd probably use a new
istream each time, with a special function in the derived
streambuf to say that I want to try again, even if I've already
seen EOF.
 
J

James Kanze

[You cut too much context...]
usually for these types of application you need to open the
file in append mode.

Append mode is really only relevant when writing, not when
reading.
You can do the following thing. write the data to the file.
find the position to where you have witten the data. next
time before writing the data, get the position where next
write will place the data.

He's not writing the data. He's reading data written (slowly)
by another application. Basically, something like "tail -f".
compare the positions, you will come to know, if any
modification

It's probable that on most systems, he can reset the error
state, do a gtell(), and save the results, and then when he
wants to retry, create a new ifstream, and gseek() to the
position returned by gtell(). Formally, I don't think it's
guaranteed (in fact, I'm pretty sure it's undefined behavior),
but pratically, I can't imagine it not working.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top