Topic: Defect Report: Wrong recommendation in DR 22
Author: Breymann@t-online.de (Ulrich Breymann)
Date: 31 Jul 01 20:18:03 GMT Raw View
[Moderator's note: this has been
forwarded to the C++ committee. -moderator.]
Defect Report: Wrong recommendation in DR22
Ref: C++ Standard Library Issues List (Revision 18)
Sec. 27.8.1.7 [lib.ifstream.members]
Issue 22. Member open vs. flags
(For your convenience, I cite DR 22 at the end of this report)
First of all, there is a typo:
basic_ifstream<>::open is meant, not basic_istream<>::open.
The latter has no open, and sec. 27.8.1.7 [lib.ifstream.members] deals
with the first.
I consider the proposed resolution
"A successful open does not change the error state."
wrong. The reasons are:
1. According to the standard, open() shall behave like fopen().
In more detail:
basic_ifstream::open() simply calls rdbuf()->open().
rdbuf() returns a pointer to a basic_filebuf-object.
You find basic_filebuf::open() in sec 27.8.1.3 [lib.filebuf.members]
on p 659.
The document says, that the opening is
"'as if' by calling std::fopen(s,modstr)"
Conclusion:
fopen() and ifstream::open() should have the same behavior.
fopen() DOES clear the state!
2. Not clearing the state means inconsistent object states.
Example:
std::ifstream is("fop.c");
char c;
while(!is.eof()) is.get(c);; // reach eof here, i.e set eof-bit
is.close();
std::cout << "reuse of existing ifstream with is.open():\n";
// consider with and without: is.clear();
is.open("anotherExistingFile.txt");
// of course, eof is not reached here! (nonempty file)
if(is.eof())
std::cout << "File cannot be read, because it seems to be at EOF,"
" although there was no read access!"
" I.e. 'is' is in an inconsistent state!\n";
else std::cout << "this is ok\n";
Of course, when I open an existing, not empty file for reading, I expect
it not to be at EOF! Following the proposed resolution above means
violating the "principle of least surprise".
I suggest:
In 27.8.1.7 [lib.ifstream.members] paragraph 3, and in 27.8.1.10
[lib.ofstream.members] paragraph 3, under open() effects, add as
the first sentence, i.e. before "Calls rdbuf->open":
Clears eofbit and failbit.
This would avoid inconsistent object states, would exhibit the expected
behavior (= no surprise), and would also conform to fopen(), as
the standard requires.
Regards
Uli
***********************************************************************
Issue 22. Member open vs. flags
Section: 27.8.1.7 [lib.ifstream.members] Status: DR Submitter:
Nathan Myers Date: 6 Aug 1998
The description of basic_istream<>::open leaves unanswered questions
about how it responds to or changes flags in the error status for the
stream. A strict reading indicates that it ignores the bits and does not
change them, which confuses users who do not expect eofbit and failbit
to remain set after a successful open. There are three reasonable
resolutions: 1) status quo 2) fail if fail(), ignore eofbit 3) clear
failbit and eofbit on call to open().
Proposed resolution:
In 27.8.1.7 [lib.ifstream.members] paragraph 3, and in 27.8.1.10
[lib.ofstream.members] paragraph 3, under open() effects, add a
footnote:
A successful open does not change the error state.
***********************************************************************
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]