Topic: while(someIOStream) "What does epression ev
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1996/07/01 Raw View
In article <4r1tvo$e1h@netlab.cs.rpi.edu> clamage@Eng.Sun.COM (Steve
Clamage) writes:
[The initial article was in comp.lang.c++.moderated, but since my
question is purely a standards one...]
|> >2) Are these tests the same (both will indicate a read past EOF)?:
|> > someStream.fail()
|> > someStream.eof()
|> No. The "fail" and "eof" tests are not equivalent. "eof" is true if
|> EOF has been reached, where the definition of "reached" is a bit
|> slippery. "fail" means the last attempted operation did not succeed,
|> for whatever reason. The two functions can return the same or
|> opposite states.
|> Example: If you attempt to read an integer, but the next available
|> input characters are "abc", the operation fails, but eof is false.
|> But if the file contained just "123" with no trailing whitespace,
|> the operation succeeds, but you have also reached eof. You should
^^^^^^
|> find "fail" is false and "eof" is true.
Should, or might? It was my impression that whether eof was set or not
in this case was not defined by the standard. (When inputting integers,
I cannot imagine an implementation in which it wasn't set, because of
the look-ahead needed. But I didn't think that the standard would
require it.)
|> If you then attempt an
|> additional input operation, it will fail (no more characxters to read)
|> and "eof" will still be true.
|> You can test "eof" before trying an operation. If it returns "true",
|> you have reached eof and no characters will be read. If "eof" is false
|> before attempting an input operation, the operation might succeed and
|> might fail; there might or might not actually be any characters left.
|> I would not test "eof" just after an input operation, since whether it
|> is true or false, the input might or might not have succeeded, as in
|> the examples above.
Question: what happens if the formatting fails? Is the implementation
still allowed to set EOF? For example:
#include <iostream.h>
#include <strstream.h>
int
main()
{
static char const s[] = "1000000000000000000000" ;
istrstream src( s ) ;
int i ;
src >> i ;
if ( src.fail() )
{
if ( src.eof() )
cout << "EOF detected" << endl ;
else
cout << "Conversion error detected" << endl ;
}
else
cout << "No error on conversion, i = " << i << endl ;
return 0 ;
}
Would a conforming implementation be allowed to output "EOF detected",
or is "Conversion error detected" 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 news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]