Doesn't ifstream::close() reset the error bits?

E

Eddie

On the VC++ 2008 Express, a short example is list below the text.

When I open the fin for the second time in the same scope, it fails.
As far as I know, the close() function reset the error bits, am I
understanding wrong?
I checked error flag and found eofbit and failbit(of course) are still
set after the second open.

ifstream::close() doesn't reset the error bits anymore? Or it never
does?
Please let me know.

Thanks in advance.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
char ch;
ifstream fin("test.txt");
if ( fin )
{
while ( fin.get(ch) )
cout << ch;
}
// Set eofbit heres
fin.close();

fin.open("test.txt"); // Reusing fin
if ( !fin )
cout << "File open error" << endl;

fin.close();

return 0;
}
 
V

Victor Bazarov

Eddie said:
On the VC++ 2008 Express, a short example is list below the text.

When I open the fin for the second time in the same scope, it fails.
As far as I know, the close() function reset the error bits, am I
understanding wrong?

You say that you *know*. Where did you get that information?

V
 
E

Eddie

In the MSDN,

void close();

Remarks

Calls the close member function for the associated filebuf object.
This function, in turn, closes the file and disconnects the file from
the filebuf object. The filebuf object is not destroyed.

The stream's error state is cleared unless the call to filebuf::close
fails.

error state doesn't include eofbit?

Please help.


Eddie said:
On the VC++ 2008 Express, a short example is list below the text.
When I open the fin for the second time in the same scope, it fails.
As far as I know, the close() function reset the error bits, am I
understanding wrong?

You say that you *know*. Where did you get that information?

V
 
J

Joshua Maurice

In the MSDN,

void close();

Remarks

Calls the close member function for the associated filebuf object.
This function, in turn, closes the file and disconnects the file from
the filebuf object. The filebuf object is not destroyed.

The stream's error state is cleared unless the call to filebuf::close
fails.

error state doesn't include eofbit?

Please help.

Try not to top post here. Instead, post your reply after the relevant
quoted text. Some people say it's cleaner, but the best reason is that
it's what's expected by readers here.

By your tests, no close() does not clear the state bits.

http://cplusplus.com/reference/iostream/ifstream/close/

Also, the reference I have states that close does not clear the state
bits.
 
J

James Kanze

On the VC++ 2008 Express, a short example is list below the text.
When I open the fin for the second time in the same scope, it
fails. As far as I know, the close() function reset the error
bits, am I understanding wrong?

Definitely. The function close() has never reset the error
bits, since you normally have to check them after the close().
(Not really on an istream, but in every case for an ostream.)
I checked error flag and found eofbit and failbit(of course)
are still set after the second open.

In the original standard, open does not reset bits either. In
the next version of the standard, a successful open will reset
any error bits (including eofbit, even if the file is empty).
This seems intuitively correct, and the fact that the original
standard doesn't do it is probably due to an oversight.
ifstream::close() doesn't reset the error bits anymore? Or it
never does?

It never did, and never will. How would you know if your close
succeeded if it did?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top