"John Harrison" <jo************ *@hotmail.com> wrote in message news:<bh******* ****@ID-196037.news.uni-berlin.de>...
No that would be wrong. fail refers to the failure to extract an element,
eof refers to a read past the end of file. It is possible for eof to be true
when fail is false. For instance suppose that last item in a file is an
integer with no following whitespace, then in order to read the integer the
program is going to have to read past the end of the file, setting eof to
true, but an element has still been extracted so fail is false and
processing should continue.
I assume that you are referring to my alternative readElem().
Assume the Langer and Kreft implementation (p. 130) of
istream_iterato r::operator++(i nt)
istream_iterato r istream_iterato r::operator++(i nt)
{
istream_iterato r tmp=*this;
readElem();
return tmp;
}
and the Stroustrup implementation (p. 529) of std::copy
Out copy(In first,In last,Out res)
{
while (first!=last) *res++=*first++ ;
return res;
}
Suppose that the last item in a file is an integer with no following
whitespace. That is, the file ends with 98 12345<eof>.
In std::copy, first does not equal last and *first++ is called. A
temporary istream_iterato r named tmp is created in which istp points
to the same stream and value equals 98. istream_iterato r::readElem()
is called. istp does not equal 0 and therefore *istp>>value is called.
istream_iterato r::value is set to 12345. ios_base::eofbi t is set but
iosbase::failbi t is not set. The function basic_ios::fail () returns
false and !(*istp>>value) returns false. Therefore, istp is not set
equal to 0. The istream_iterato r returned from
istream_iterato r::operator++(i nt) is the temporary that was created.
In the next iteration, first does not equal last as istp does not
equal 0. *first++ is called. A temporary is created in which istp
points to the same stream and value equals 12345.
istream_iterato r::readElem() is called. istp does not equal 0 and
therefore *istp>>value is called. The extraction fails as there is
nothing to extract. The iosbase::failbi t is set, basic_ios::fail ()
returns true, and !(*istp>>value) returns true. istp is set equal to
0. The istream_iterato r returned from
istream_iterato r::operator++(i nt) is the temporary that was created
that contains value equal to 12345.
In the next iteration, first equals last as istp equals 0 and res is
returned from the copy function.
Is this the correct interpretation of what happens when the end of the
file is reached? If so, I don't see anything wrong with the
alternative:
void readElem()
{
istp!=0&&istp->good()?*istp>> value:istp=0;
}