Title
Closing an fstream should clear error state
Status
cd1
Section
[ifstream.members] [ofstream.members]
Submitter
Nathan Myers

Created on 2003-06-03.00:00:00 last changed 164 months ago

Messages

Date: 2010-10-21.18:28:33

[ Post-Sydney: Howard provided a new proposed resolution. The old one didn't make sense because it proposed to fix this at the level of basic_filebuf, which doesn't have access to the stream's error state. Howard's proposed resolution fixes this at the level of the three fstream class template instead. ]

Date: 2010-10-21.18:28:33

[ Kona: the LWG agrees this is a good idea. Post-Kona: Bill provided wording. He suggests having open, not close, clear the error flags. ]

Date: 2010-10-21.18:28:33

Proposed resolution:

Change [ifstream.members], para. 3 from:

Calls rdbuf()->open(s,mode|in). If that function returns a null pointer, calls setstate(failbit) (which may throw ios_base::failure [Footnote: (lib.iostate.flags)].

to:

Calls rdbuf()->open(s,mode|in). If that function returns a null pointer, calls setstate(failbit) (which may throw ios_base::failure [Footnote: (lib.iostate.flags)), else calls clear().

Change [ofstream.members], para. 3 from:

Calls rdbuf()->open(s,mode|out). If that function returns a null pointer, calls setstate(failbit) (which may throw ios_base::failure [Footnote: (lib.iostate.flags)).

to:

Calls rdbuf()->open(s,mode|out). If that function returns a null pointer, calls setstate(failbit) (which may throw ios_base::failure [Footnote: (lib.iostate.flags)), else calls clear().

Change [fstream.members], para. 3 from:

Calls rdbuf()->open(s,mode), If that function returns a null pointer, calls setstate(failbit), (which may throw ios_base::failure). (lib.iostate.flags) )

to:

Calls rdbuf()->open(s,mode), If that function returns a null pointer, calls setstate(failbit), (which may throw ios_base::failure). (lib.iostate.flags) ), else calls clear().

Date: 2003-06-03.00:00:00

A strict reading of [fstreams] shows that opening or closing a basic_[io]fstream does not affect the error bits. This means, for example, that if you read through a file up to EOF, and then close the stream and reopen it at the beginning of the file, the EOF bit in the stream's error state is still set. This is counterintuitive.

The LWG considered this issue once before, as issue 22, and put in a footnote to clarify that the strict reading was indeed correct. We did that because we believed the standard was unambiguous and consistent, and that we should not make architectural changes in a TC. Now that we're working on a new revision of the language, those considerations no longer apply.

History
Date User Action Args
2010-10-21 18:28:33adminsetmessages: + msg2528
2010-10-21 18:28:33adminsetmessages: + msg2527
2010-10-21 18:28:33adminsetmessages: + msg2526
2003-06-03 00:00:00admincreate