Thread: Ok, this should be easy...

  1. #1
    Registered User
    Join Date
    Jan 2003

    Lightbulb Ok, this should be easy...

    Alright guys, here's an an easy one...maybe you can let me know what the heck is going on here. For some reason, when I attempt to open an ifstream using a filename that doesn't exist, .bad() returns 0. Here's the exact code:

    void LoadBankAccounts()

    ifstream BankAccounts("accounts.dat", ios::nocreate);
    cout << "Couldn't load file..." << endl;

    accounts.dat DOES NOT EXIST!!! I have stepped through the program and for some reason the (state & badbit) expression is returning zero. Any ideas?

  2. #2
    Registered User
    Join Date
    Jan 2003

    Angry Arg...I confused

    Ok, I switched to using .fail() and it returns a non-zero value so I've got my solution, but why the heck isn't .bad() working? Is there something I'm not aware of? Like I said, .fail() works, but just curious as to why .bad() doesn't or if it should be used elsewhere for other reasons. Thanks.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Do you actually know what .bad() returns?
    Maybe it isn't the status of the file load

  4. #4
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    int bad() const;
    Returns a nonzero value to indicate a serious I/O error. This is the same as setting the badbit error state. Do not continue I/O operations on the stream in this situation.

    Not being able to open a file probably isn't classified as "serious". *shrug*

    &nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;___&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;/\&nbsp;\&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;/\_&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;\_\&nbsp;\/\_\&nbsp;&nbsp;____&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ __\//\&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;__&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;I&nbsp;have&nbsp;a&nbsp;BAD&nbsp;figlet& nbsp;addiction.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Just for future reference you can also use the fstream member function is_open()

    bad() returns true if an unrecoverable error occurs. You can recover from an unopenable file by opening another one.

    You can also use good() to simply see if the stream is good for I/O operations.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:48 PM
  2. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  3. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  4. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  5. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM