Thread: File.is_open() or File.fail() ?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    160

    File.is_open() or File.fail() ?

    What's best?
    if ( File.is_open() ) or
    if (! File.fail() )

    What I mean is, do these functions do more then the obvius which I should know about hwne chusing.

    I'm asking because fail() is const and is_open isn't.
    Last edited by Zahl; 10-19-2002 at 07:37 AM.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    well in your case may be not.. but conside a program where you have to check if a file is opened... So here u use the 2nd one and the fail one is used to see if a file failed to open...

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Sounds like the same thing.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    So File.is_open() == !File.fail() ???
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    no they are not the same in the context i have used them.... See i write a program to check the files that are currently opened by the user.... and here i would try to open each file and check if it failed.. I would just like to check if it is opened.....

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I always write like this:
    Code:
     
    ifstream fin("file.txt");
    if (fin)
    {
      ...
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    hmm... didn't know you could do that... what kind of overloated operater have they used to do that?
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  8. #8
    good question. I have wondered that myself. Is it something like overloading "if" or "!" ?

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Inquirer
    good question. I have wondered that myself. Is it something like overloading "if" or "!" ?

    ~Inquirer
    Not too sure...but I think it may be operator void*() which returns NULL if the failbit is set

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    but void functions doesn't return anything. That's the point about void.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Zahl
    but void functions doesn't return anything. That's the point about void.
    (void) != (void*)

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    why doesn't it just return *, and doesn't * mean that it's somekind of pointer... well then a pointer to what.
    String * or Char * or int * or...
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  13. #13

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    To answer the OP's question, I prefer to be explicit in what I want so there is no confusion such as in this thread.
    Code:
    std::ifstream input_file ( "something.ext" );
    
    if ( input_file.is_open() ) {
      // Work with the file.
    }
    else {
      // Report the error and handle
    }
    -Prelude
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Hey Prelude, maybe you, too, could ansver a question I maid two days ago here http://cboard.cprogramming.com/showt...threadid=26709 but never got an ansver on.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

Popular pages Recent additions subscribe to a feed