Thread: fstream questions!

  1. #1
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100

    fstream questions!

    So in the constructor for my class, i want it to check to see if the list file is there list.bin if so i want it to open it, else wise, i'd like it to create it. I also want it to ignore it if it's empty, but it doesn't seems to work.

    Code:
        std::ifstream fin("list.bin", ios::binary); //open it to read? Can this also create it?
        if (!fin){ // if it doesn't exist? this works.
            wxMessageBox("File Function Failed!"); 
        }
        else {  // if it exists...
            if(!fin.eof()){ // .. but what if it's empty?
                wxMessageBox("NOT EMPTY!"); // this happens with a seemingly empty file!
                
            }
            else {
                wxMessageBox(" EMPTY!"); 
                fin.close(); // this is in the wrong place, ignore it!
            }
        }
    Last edited by Terran; 07-04-2008 at 05:47 PM.
    Sorry, but i'm a Code::Blocks man now.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There's no point in opening a file for reading and creating it if it doesn't exist, so the ifstream doesn't do that. I'd check to see if the file opened. If it didn't then create with an ofstream. If it did, read it.

    As far as your problem with eof(), it will never return true with a new file stream until after you've attempted to read. You have to do some read operation first, and that read operation has to attempt to read when there are no more characters, then eof() will be true.

  3. #3
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Quote Originally Posted by Daved View Post
    There's no point in opening a file for reading and creating it if it doesn't exist, so the ifstream doesn't do that. I'd check to see if the file opened. If it didn't then create with an ofstream. If it did, read it.

    As far as your problem with eof(), it will never return true with a new file stream until after you've attempted to read. You have to do some read operation first, and that read operation has to attempt to read when there are no more characters, then eof() will be true.
    Ahh then that's ok, cause i was thinking i'd get an error if tried to read from a non-existent file. So really i can just try to read the data and it's no big deal if it doesn't read it.
    Sorry, but i'm a Code::Blocks man now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM