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.
Printable View
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.
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...
Sounds like the same thing.
So File.is_open() == !File.fail() ???
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.....
I always write like this:
Code:
ifstream fin("file.txt");
if (fin)
{
...
}
hmm... didn't know you could do that... what kind of overloated operater have they used to do that?
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 setQuote:
Originally posted by Inquirer
good question. I have wondered that myself. Is it something like overloading "if" or "!" ?
~Inquirer
but void functions doesn't return anything. That's the point about void. :)
(void) != (void*)Quote:
Originally posted by Zahl
but void functions doesn't return anything. That's the point about void. :)
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...
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. ;)
-PreludeCode:std::ifstream input_file ( "something.ext" );
if ( input_file.is_open() ) {
// Work with the file.
}
else {
// Report the error and handle
}
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.
Sorry, I can't reproduce the problem on either MSVC++ or Borland.
-Prelude
Funny since I use Borland 6.0... Glad you tryed though. It just doesn't make much sense that when I use the for loop to set every part of the array equal '\0' it works. Especially since this shoudln't be the way to do it. (or necesary)