Unexpected behavior of the istream::peek function

In the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
    cout << "Enter\n";
    char s[20];
    cin.get(s, 10);
    char c = cin.peek();
    cout << c << '\n';
    cout << "The state is " << cin.rdstate() << '\n';
    cin.ignore(11, '\n');
    cout << "The state is " << cin.rdstate() << '\n';
    char e = cin.peek();
    cout << "The state is " << cin.rdstate() << '\n';
    cout << e <<'\n';
    return 0;
}


Assume that the Input is
AyushAgarwal21Single

The output generated is:

w
The state is 0
The state is 0


After using the ignore() function, all the characters from the input stream are extracted. When I use the peek() function in the 13th line, it does not return eofbit as it should.

http://www.cplusplus.com/reference/istream/istream/peek/

Instead, it waits for an input. Why does this happen?
I dont know.
Last edited on
Instead, it waits for an input. Why does this happen?
Because you are trying to read a char that isn't in the stream so it waits for input.
as peek() is supposed to do:
it returns the next character in the stream without extracting it.
this implies that a character exists in the stream for this function to return.
if there isn't a character, it waits until a character is pushed onto the stream buffer.
Try creating a file with the exact data you want (and make sure you don't have a newline there at the end). Then run your program by piping the file in:

myprog < myfile

Hope this helps.
@naraku9333 and @Rechard3 The peek() function should return an EOFbit when the stream is empty. I've confirmed that on several sites and books. It isn't supposed to ask for input. Why does it do that?

@Duoas I'll try that.
@ChosenTorture:
peek() when used by a stream connected to a file (not stdin), will return EOF.
console works a bit different, console doesn't have an end for input, input from stdin will never reach eof, that's why it will wait for input.
ChosenTorture wrote:
The peek() function should return an EOFbit when the stream is empty.

Correction: the peek() function will return EOF when the read pointer reaches the end of the stream.

The standard input stream was never terminated, therefore you cannot reach the end of input, therefore your program will wait for input until you close it (with a Ctrl-Z (on Windows) or a Ctrl-D (on nixen)).

This is true of any stream.
@Duoas, @Rechard3 and @naraku9333

I'm a bit confused here.

http://en.cppreference.com/w/cpp/io/basic_istream/peek

This page says that the peek() function will return Traits::eof() or badbit is set when an exception occurs.

http://www.cplusplus.com/reference/istream/istream/peek/

This page says that "If there are no more characters to read in the input sequence or If any internal state flags is already set before the call or is set during the call, the function returns the end-of-file value (EOF)."

Also, I found out that eofbit isn't set in the case of peek() now. Instead, Traits::eof() is set.

http://en.cppreference.com/w/cpp/io/ios_base/iostate

In this page in the The eofbit section, peek() is not listed. Thus it may mean that peek() never returns eofbit but uses Traits::eof instead.

I also own the book The C++ STL: A Tutorial and a Reference by Nicolai Josuttis. It says that:

"Returns EOF if no more characters can be read. EOF is the value returned from Traits::eof(). For istream, this is the constant EOF."

[b]Who is trying to say what exactly?[/b]

@Duoas: You said that
"The standard input stream was never terminated, therefore you cannot reach the end of input, therefore your program will wait for input until you close it".


How am I supposed to peek() into the input stream if I close the program or terminate the input stream? Also, how do you terminate the input stream?

@Rechard3: Are you trying to say that streams never reach an eof state?

Thanks for pointing out the omission in http://en.cppreference.com/w/cpp/io/ios_base/iostate -- fixed now.
(on peek() page of cppreference.com, you could click on UnformattedInputFunction to learn when it sets the eofbit)
Last edited on
Huh. Ok. That correction clears one of the doubts. I still don't understand why peek() waits for an input in the 14th line of the code. Also, what does reaching the end of the stream means? Even if I provide a shorter input than the one provided above, the peek() function still waits for an input. How does one reach the end of the stream if I can't reach it even after extracting and discarding every character in the stream?

@ChosenTorture
I'm starting to find your subborn attitude a bit annoying, particularly when you pull out stuff written from the standards to back up your (incorrect) reasoning.

You are telling us that there are no more characters to be read in an open standard input stream.

We are telling you -- and by we, I mean people who have been eating and drinking and sleeping and pooping computers since they first frobbed a keyboard at least 25 or 30 years ago -- that your concept of "no more characters to be read" is WRONG.

There most certainly ARE more characters to be read. They just haven't been input yet.


Your noobishness is showing when you start quibbling EOF vs Traits::eof() vs eofbit vs whatever. It's the same damn thing.


How am I supposed to peek() into the input stream if I close the program or terminate the input stream?

WTH?!! You've been spouting it at us for days.

If the program is closed/input stream is terminated (== same damn thing), you cannot peek(), because it will return EOF and or your program isn't running any code to do the stupid peek, right?


Also, how do you terminate the input stream?

I already answered your question. Pay attention.
http://www.cplusplus.com/forum/general/105966/#msg573687


I've had enough of this topic, so bye now. Good luck with your circular reasoning and arrogant tell the world how logic works crap. If you want to run with the big boys, you might have to put away the little boy way of thinking.
@ChosenTorture
Are you trying to say that streams never reach an eof state?


no, i'm saying that cin and cout never reach eof.
Topic archived. No new replies allowed.