Checking ifstream state

Discussion in 'C++' started by Fraser Ross, Jun 18, 2004.

  1. Fraser Ross

    Fraser Ross Guest

    Can I do several reading operations and then check fail() or do I need to
    check fail() after every reading operation. Is it possible than a read
    operation can set fail() and the next not? I am reading from a character
    based stream in binary mode.

    Fraser.
     
    Fraser Ross, Jun 18, 2004
    #1
    1. Advertising

  2. Fraser Ross wrote:
    >
    > Can I do several reading operations and then check fail() or do I need to
    > check fail() after every reading operation. Is it possible than a read
    > operation can set fail() and the next not?


    No.
    When a stream has gone into a fail state, it will refuse working until
    you clear that fail state.
    So once a read operation has failed all subsequent read operations will
    also fail.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jun 18, 2004
    #2
    1. Advertising


  3. > Can I do several reading operations and then check fail() or do I need to
    > check fail() after every reading operation. Is it possible than a read
    > operation can set fail() and the next not? I am reading from a character
    > based stream in binary mode.


    There is a big difference between "bad()", "fail()" and "good()". Bjarne
    Stroustrup explains the stream state in his book:

    (back translated from the german translation :)

    == snip
    If good() or eof() is set, the previous input operations are a success.
    If good() is set, the next input operation could by successful. If
    good() isn't set, the next input operation will fail for sure. Each
    input operation an a stream that isn't "good()" has no effect.
    (...)
    The difference between bad() and fail() is subtly. Is the state fail()
    but not bad(), then it's possible that the stream is still usable and no
    characters are lost. Is the stream state bad() the stream state is fully
    undefined.
    == endsnip

    hope this will help

    best regards
    Tobias
     
    Tobias Erbsland, Jun 18, 2004
    #3
  4. In message <>, Fraser Ross
    <fraserATmembers.v21.co.unitedkingdom@?.?.invalid> writes
    >Can I do several reading operations and then check fail() or do I need to
    >check fail() after every reading operation. Is it possible than a read
    >operation can set fail() and the next not? I am reading from a character
    >based stream in binary mode.
    >

    Once the fail state is set, it remains set until you clear it explicitly
    by calling clear(). It's not even cleared by closing the file and
    opening another, which often catches the unwary.

    --
    Richard Herring
     
    Richard Herring, Jun 18, 2004
    #4
  5. If I know correctly, there is also some exception support that can be
    activated. In this case, you don't have to check the state after each
    operation, but you can catch the exception in a try catch block...

    Catalin

    "Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
    news:...
    > Can I do several reading operations and then check fail() or do I need to
    > check fail() after every reading operation. Is it possible than a read
    > operation can set fail() and the next not? I am reading from a character
    > based stream in binary mode.
    >
    > Fraser.
    >
    >
     
    Catalin Pitis, Jun 18, 2004
    #5
  6. Fraser Ross

    Fraser Ross Guest

    "Tobias Erbsland"
    > If good() or eof() is set, the previous input operations are a success.


    Not necessarily. When extraction is done character-wise eof will be set
    when an attempt is made to read past the last character and fail will be set
    in conjunction.

    Fraser.
     
    Fraser Ross, Jun 18, 2004
    #6
    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. David Lamb
    Replies:
    1
    Views:
    683
  2. Weng Tianxiang
    Replies:
    7
    Views:
    1,111
    Mike Treseler
    Nov 25, 2003
  3. shamanthakamani
    Replies:
    1
    Views:
    3,498
    Natty Gur
    Nov 20, 2003
  4. Not Liking Dot Net Today
    Replies:
    0
    Views:
    616
    Not Liking Dot Net Today
    Apr 21, 2004
  5. Vinay Joseph Mathew

    Losing Session State on a State Server

    Vinay Joseph Mathew, Aug 10, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    600
    =?Utf-8?B?c29ubnk=?=
    Sep 16, 2004
Loading...

Share This Page