Does calling ifstream::close
reset the stream's failbit
and/or badbit
, similar to calling clear
? This is not a duplicate of this question- I need to know whether the flags are reset, not just when they're set.
For example, I'm using something like the following if-else
in my current project:
ifstream myInputStream("foo.txt");
//Let's pretend this compiles all the time, even though it
//might not because the operator is ambiguous.
myInputStream << "Outputting to an input stream causes problems."
if (myInputStream.fail())
{
cout << "Aw, nuts. It failed." << endl;
myInputStream.close();
return false;
}
else
{
cout << "No nuts here. Only chocolate chips." << endl;
myInputStream.close();
return true;
}
Do I have to have the call to myInputStream.close
after the call to myInputStream.fail
, in each branch, to get an accurate check? Or will this work:
ifstream myInputStream("foo.txt");
myInputStream << "Outputting to an input stream causes problems.";
myInputStream.close();
if (myInputStream.fail())
{
cout << "Aw, nuts. It failed." << endl;
return false;
}
else
{
cout << "No nuts here. Only chocolate chips." << endl;
return true;
}
I know that ifstream::close
can itself set failbit
or badbit
if closing fails, which is one reason I want to call it before checking for failure- I need to return false regardless of what caused it. It also looks less cluttered if the closure is only done once.
tl;dr;
Does ifstream::close
reset failbit
or badbit
if something else has already set it, making my second code sample return true?