don't sync() on an fstream at EOF

Discussion in 'C++' started by kartik, Oct 30, 2004.

  1. kartik

    kartik Guest

    I open an fstream in read-only mode, read till the end, then try to
    sync() before seeking to position 0 & reading again. But the sync
    fails. I discovered that clear()ing the stream before the sync causes
    the sync to work. Seeking to position 0 before the sync doesn't seem
    to help.

    If you look at sync() as simply invalidating the read buffer (isn't
    that right?), isn't it illogical for it to fail in this situation?

    Besides sync() doesn't flush the write buffer (on a read-write
    stream), does it? Similarly, flush() doesn't invalidate the read
    buffer, does it?

    I'm using g++ 3.3.2 on Mandrake Linux 10; kernel 2.6.

    -kartik
    kartik, Oct 30, 2004
    #1
    1. Advertising

  2. kartik

    Jack Klein Guest

    On 29 Oct 2004 21:46:40 -0700, (kartik)
    wrote in comp.lang.c++:

    > I open an fstream in read-only mode, read till the end, then try to
    > sync() before seeking to position 0 & reading again. But the sync
    > fails. I discovered that clear()ing the stream before the sync causes
    > the sync to work. Seeking to position 0 before the sync doesn't seem
    > to help.
    >
    > If you look at sync() as simply invalidating the read buffer (isn't
    > that right?), isn't it illogical for it to fail in this situation?
    >
    > Besides sync() doesn't flush the write buffer (on a read-write
    > stream), does it? Similarly, flush() doesn't invalidate the read
    > buffer, does it?
    >
    > I'm using g++ 3.3.2 on Mandrake Linux 10; kernel 2.6.
    >
    > -kartik


    Attempting to read past the end of file sets the fail bit on the
    stream. All operations on the stream, including sync(), will fail
    until clear() is used to remove the fail bit.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Oct 30, 2004
    #2
    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. Armando
    Replies:
    6
    Views:
    745
    Armando
    Jan 29, 2004
  2. Kobu
    Replies:
    10
    Views:
    620
    Keith Thompson
    Mar 4, 2005
  3. Greg
    Replies:
    7
    Views:
    572
    Default User
    Dec 19, 2005
  4. David
    Replies:
    2
    Views:
    584
    Kumar Anurag
    Feb 11, 2010
  5. Trans
    Replies:
    2
    Views:
    478
    Trans
    Dec 12, 2005
Loading...

Share This Page