Thread: When does ios::badbit get set?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question When does ios::badbit get set?

    When and why does ios::badbit get set on an input or output operation on a file with an ifstream or ofstream?
    I did a little Googling to try to answer this on my own, but didn't really find anything explaining in detail when and why this happens.

    Thanks in advance.

    EDIT: Hmm...it looks like one of the reasons is hardware failure. I hope that doesn't indicate there's something wrong with my computer...
    Last edited by Programmer_P; 01-14-2011 at 11:15 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Whenever something bad has happened, that's when badbit gets set. End of file. Matching failure (i.e. trying to read a number when the next thing in the file is a letter). File not existing.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    Whenever something bad has happened, that's when badbit gets set. End of file. Matching failure (i.e. trying to read a number when the next thing in the file is a letter). File not existing.
    Ok, so say a getline(input_stream, buffer_str) operation had a matching failure...what could I do to fix that?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    getline doesn't match, it just gets bytes. If getline fails, that's because the file is empty, at end of file, or could not be read.

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    getline doesn't match, it just gets bytes. If getline fails, that's because the file is empty, at end of file, or could not be read.
    And let's say it couldn't be read...
    What are some of the reasons that might happen?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Programmer_P View Post
    And let's say it couldn't be read...
    What are some of the reasons that might happen?
    1) You never bothered to open the file.

    2) You don't have permissions to read the file. (The open should have failed in that case.)

    3) Bad fs.

    4) Aliens.

    5) End of file/empty file.

    6) You misspelled the filename or specified the wrong directory.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    1) You never bothered to open the file.

    2) You don't have permissions to read the file. (The open should have failed in that case.)

    3) Bad fs.

    4) Aliens.

    5) End of file/empty file.

    6) You misspelled the filename or specified the wrong directory.
    Hehe. I meant reasons beyond the obvious.
    Obviously, I opened the file, therefore 2 and 6 don't apply either, and the file is definitely not empty. And I know for a fact it didn't get to the end of the file when badbit was set, which rules out 5 as well.

    And why would a filestream be bad? I wont even bother to ask about the aliens...
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Programmer_P View Post
    Hehe. I meant reasons beyond the obvious.
    Obviously, I opened the file, therefore 2 and 6 don't apply either
    Did you check that the open succeeded? Are you reading any part of the file?

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    Did you check that the open succeeded? Are you reading any part of the file?
    Indeed. I'm reading most of the file, but it stops somewhere after line 1052. The file has 1115 lines.

    EDIT: Nevermind. Its reading the whole file now.
    It seems like it was a temporary thing, now its gone.
    Last edited by Programmer_P; 01-14-2011 at 11:51 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you using anything other than getline? What operation is the last successful one, what operation is the first unsuccessful one? Are you using the C-string version of getline, or the std::string version? getline will set failbit if you try to overflow your buffer (i.e. read more characters than can be stored safely).

  11. #11
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    Are you using anything other than getline?
    No, not for input operations.
    What operation is the last successful one, what operation is the first unsuccessful one?
    I can't answer that, because its reading the whole file now. The problem appears to be gone. badbit is no longer being set.
    Are you using the C-string version of getline, or the std::string version? getline will set failbit if you try to overflow your buffer (i.e. read more characters than can be stored safely).
    I'm using a std::getline version, which doesn't allow you to specify a buffer_size.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I recall someone giving you advice in ancient times, that you have ensure success rather than assume things worked in programming by checking return values and other things, which you casually ignored because you were too new or something. You would be able to tell us much more about how your program is actually working if you followed through on that advice.

    And, well, we're telepathically solving a badbit problem... but whatever.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling errors and using Cmake Set Policy
    By eligor in forum Linux Programming
    Replies: 2
    Last Post: 01-25-2010, 01:43 PM
  2. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  3. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. Set default directory with GetTempDir?
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 05-04-2003, 11:36 PM