Thread: Checking to make sure a file opened

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    20

    Checking to make sure a file opened

    I've tried a variety of B.S. ways that I concocted to try and check if a file is opened after I try to open it. A couple of them work, but it's really sloppy code and probably uneccessary. Is there a standard way to check?

    I'm opening my file like this:

    ifstream myFile;
    myFile.open("myFile.dat");

    Thanks!

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Code:
    if (myFile.fail())
    {
    ...
    }
    else
    {
    ...
    }

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    20
    Great, thanks! This is why I'm always so nervous about writing my own functions to do these things...it's usually already done for me.

    Thanks again,
    Kevin

  4. #4
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    As i recently learned, the fail() function will not return true until eof is found, and there are often better ways to do this. My example will work, but a quick review of my thread link below may help to elaborate on the topic:

    http://cboard.cprogramming.com/showt...threadid=35914

    Also, as far as writing your own functions go, its always a good idea for practice. Writing your own functions to do things, wether they can already be done through a pre-made function, is a good way to get a understanding for C++ and how things work.

    Happy coding!

    -RoD
    Last edited by RoD; 03-14-2003 at 02:30 PM.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    20
    That's strange...how can it find EOF if it can't even open it?

  6. #6
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    It cant. I was speaking in terms of some different implementations of the fail function. In the case the above link presents, it is used in a while loop as a condition for the loop. When used in that instance extra code was needed to make the function act as i wished.

    Sorry to confuse,

    -RoD

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    20
    Ok, I understand I think. Just to make sure I understand though, the statement:

    if (myFile.fail())
    {
    //code
    }

    Will work fine on its own, but when used in the context of a loop it must find EOF before it will return true?

  8. #8
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    To my knowledge thats correct, yes.

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    20
    Great, thanks for all your help. It's the little things like this that really make me better.

  10. #10
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Ditto, and anytime.

    -RoD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM