Topic: string extractor vs. EOF
Author: rice@tiac.net (Kenneth Rice)
Date: 1996/08/04 Raw View
Suppose you execute the following code:
ifstream in("foo");
string s;
in >> s;
if (in.good())
{
// Do we get here?
}
With a data file that contains a single word - "word" - where the 'w' is
the first character in the file and the 'd' is the last characther.
Should the state of the stream be good?
If not, should it be failbit, eofbit, or badbit?
If the status is not supposed to be good, how can the caller distinguish
between the above mentioned case and the case where data file is empty?
Sec 21.2.1.8.9 [lib.string.io] in the june working paper is unclear on this.
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1996/08/05 Raw View
rice@tiac.net (Kenneth Rice) writes:
> Suppose you execute the following code:
>
> ifstream in("foo");
> string s;
> in >> s;
> if (in.good())
> {
> // Do we get here?
> }
>
> With a data file that contains a single word - "word" - where the 'w' is
> the first character in the file and the 'd' is the last characther.
>
> Should the state of the stream be good?
Undefined. This may vary from one implementation to the next, or even
between streams on the same implementation.
> If not, should it be failbit, eofbit, or badbit?
The state of eofbit is not defined. As far as I can tell, neither
failbit nor badbit should be set.
> If the status is not supposed to be good, how can the caller distinguish
> between the above mentioned case and the case where data file is empty?
By testing using "fail", or with the conversion operators (which are
based on fail, not good).
If you think that this makes the functions good, and the eofbit, pretty
useless, join the club.
> Sec 21.2.1.8.9 [lib.string.io] in the june working paper is unclear on this.
> From the description of the meaning of failbit in sec 27.4.3.1.3
> [lib.ios::iostate] sounds like it isn't appropriate in this case:
> "failbit indicates that an input operation failed to
> read the expected characters, or that an out-
> put operation failed to generate the desired
> characters."
>
> (I have access to two library implementations, one of which leaves the
> state of the stream as good, the other sets it to failbit after reading
> the 4 chars into the string.)
It the implementation sets the fail bit, then it is in error, at least
as far as I can see. Most implementations I'm familiar with *will* set
eof in the described case, at least when reading from a file, but this
is not guaranteed.
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]