Thread: bad and fail of steam

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    bad and fail of steam

    Hello everyone,


    Two questions,

    1. Confused about what is the differences between bad and fail. This what Bjarne's comments in his book, but I do not quite understand and it is appreciated if you could share some best practices of the differences and when to use them.

    --------------------
    The difference between the states fail() and bad() is sutle. When the state is fail() but not also bad(), it is assumed that the stream is uncorrupted and that no characters have been lost. When the state is bad(), all bets are off.
    --------------------

    2. when do you prefer to use bad()/fail() and when do you prefer to use setting exceptions (std::ios::badbit or std::ios::failbit or both).


    thanks in advance,
    George

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    1. You might be interested in some of the functions listed here; particularly, good, fail, bad, and eof to at least learn how and what bits they check. Your standard streams will have these functions.

    The statement that Stroustrup made basically says that the fail- and badbits can be set for a variety of reasons. For instance, eofbit is set after you've reached the end of a file. The failbit is probably set when an input conversion fails, but that could have occurred simply because there was no input to be read (meaning that eof() would return true). The stream is not corrupted, but empty, in this case.

    2. It depends on the situation.

    I've depended on the stream being good() before I use it on certain occassions, like writing stream inserters and extracters for my classes. Sometimes I've checked if some input routine stopped because there was no more input, and assumed that something bad happened if that was not the case. But it really depends on how your program works.

    I've always relied on the interface to manipulate the bits for me.
    Last edited by whiteflags; 02-17-2008 at 08:50 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    2. I assume you are asking because you plan on writing your own streams? Is this a hypothetical question, or do you have your own stream in mind? One example might be a stream that reads from a socket. If the socket connection fails or data is lost, then perhaps the badbit is set. If there is a conversion failure or no more data on the socket then perhaps failbit will be set.

    The standard streams will rarely set the badbit in practice. The failbit is often set, which is why the while (read from stream) idiom is used. The basic instance is when you attempt a read and that read fails. Perhaps it failed because the stream is empty and it could not read into the variable, or maybe the data in the stream was in the wrong format.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks citizen,


    Quote Originally Posted by citizen View Post

    I've always relied on the interface to manipulate the bits for me.
    What interface do you mean?


    Thanks Daved,


    Quote Originally Posted by Daved View Post

    The standard streams will rarely set the badbit in practice. The failbit is often set, which is why the while (read from stream) idiom is used.
    I can not see the relationship between "rarely set the badbit in practice" and using while loop. Could you show more description please?

    BTW: even if there are frequently badbit dues to unreliable stream (like socket in a roaming network), we can test for badbit and break the loop, right?


    regards,
    George

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> What interface do you mean?

    A stream's manipulators, and the functions it inherits from the basic_ios class that involve the state of the stream. I never needed to set the bits myself, but if you're writing your own stream, I can see why you'd ask.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I can not see the relationship between "rarely set the badbit in practice" and using while loop. Could you show more description please?

    There is no relationship. The two sentences were separate thoughts. The while (read from stream) idiom catches either error (but not eof). So testing the return value of the read will handle badbit issues if they occur.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi citizen,


    I am making my class streamable, is what I am doing covers your below point?

    Quote Originally Posted by citizen View Post
    >> What interface do you mean?

    A stream's manipulators, and the functions it inherits from the basic_ios class that involve the state of the stream. I never needed to set the bits myself, but if you're writing your own stream, I can see why you'd ask.

    regards,
    George


    Thanks for your clarification, Daved!


    Quote Originally Posted by Daved View Post
    >> I can not see the relationship between "rarely set the badbit in practice" and using while loop. Could you show more description please?

    There is no relationship. The two sentences were separate thoughts. The while (read from stream) idiom catches either error (but not eof). So testing the return value of the read will handle badbit issues if they occur.

    regards,
    George

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> I am making my class streamable, is what I am doing covers your below point?

    Sure it does. To do that, you will have to define operators << for inserting an object you made in the stream, and maybe a >> operator if you know how to extract it from a stream.

    There are ways to learn how to do that on the internet, so mostly, I'd just give it a try George, and come back when you have something to fix.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks citizen,


    Up to now, my current questions about auto_ptr is answered.

    Quote Originally Posted by citizen View Post
    >> I am making my class streamable, is what I am doing covers your below point?

    Sure it does. To do that, you will have to define operators << for inserting an object you made in the stream, and maybe a >> operator if you know how to extract it from a stream.

    There are ways to learn how to do that on the internet, so mostly, I'd just give it a try George, and come back when you have something to fix.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Computer will not boot.
    By RealityFusion in forum Tech Board
    Replies: 25
    Last Post: 09-10-2004, 04:05 PM
  2. Debug Assertion Failure - bad pointer
    By ventolin in forum C++ Programming
    Replies: 5
    Last Post: 05-24-2004, 10:10 AM