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

Discussion in 'C++' started by roughtrader, Feb 16, 2009.

  1. roughtrader

    roughtrader Guest

    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
     
    roughtrader, Feb 16, 2009
    #1
    1. Advertising

  2. roughtrader

    Ramaswamy Guest

    Re: fstream data from file, with file periodically written with newdata

    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
     
    Ramaswamy, Feb 16, 2009
    #2
    1. Advertising

  3. roughtrader

    James Kanze Guest

    Re: fstream data from file, with file periodically written with newdata

    On Feb 16, 8:12 pm, roughtrader <> wrote:

    > 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.


    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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 17, 2009
    #3
  4. roughtrader

    James Kanze Guest

    Re: fstream data from file, with file periodically written with newdata

    On Feb 16, 8:39 pm, Ramaswamy <> wrote:

    [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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 17, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve C. Orr, MCSD
    Replies:
    1
    Views:
    598
    reaway lee
    Aug 24, 2003
  2. Armando
    Replies:
    6
    Views:
    779
    Armando
    Jan 29, 2004
  3. Pkid
    Replies:
    2
    Views:
    325
  4. carmelo
    Replies:
    24
    Views:
    815
    Tom Anderson
    Oct 13, 2008
  5. KaiWen
    Replies:
    102
    Views:
    2,887
    Jorgen Grahn
    Sep 15, 2011
Loading...

Share This Page