I was looking at this article on Cplusplus.com, http://www.cplusplus.com/reference/iostream/istream/peek/
I'm still not sure what peek() returns if it reaches the end of the file.
In my code, a part of the program is supposed to run as long as this statement is true
(sourcefile.peek() != EOF)
where sourcefile is my ifstream.
However, it never stops looping, even though it has reached the end of the file.
Does EOF not mean "End of File"? Or was I using it wrong?
Consulting the Standard,
Returns:
traits::eof()
ifgood()
isfalse
. Otherwise,returnsrdbuf()->sgetc()
.
As for sgetc()
,
Returns: If the input sequence read position is not available, returns
underflow()
.
And underflow
,
If the pending sequence is null then the function returns
traits::eof()
to indicate failure.
So yep, returns EOF
on end of file.
An easier way to tell is that it returns int_type
. Since the values of int_type
are just those of char_type
plus EOF, it would probably return char_type
if EOF weren't possible.
As others mentioned, peek
doesn't advance the file position. It's generally easiest and best to just loop on while ( input_stream )
and let failure to obtain additional input kill the parsing process.