public member function
<istream> <iostream>

std::istream::peek

int peek();
Peek next character
Returns the next character in the input sequence, without extracting it: The character is left as the next character to be extracted from the stream.

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).

Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to true). Then (if good), it reads one character from its associated stream buffer object by calling its member function sgetc, and finally destroys the sentry object before returning.

Calling this function sets the value returned by gcount to zero.

Parameters

none

Return Value

The next character in the input sequence, as a value of type int.

If there are no more characters to read in the input sequence, or if any internal state flags is set, the function returns the end-of-file value (EOF), and leaves the proper internal state flags set:

flagerror
eofbitNo character could be peeked because the input sequence has no characters available (end-of-file reached).
failbitThe construction of sentry failed (such as when the stream state was not good before the call).
badbitError on stream (such as when this function catches an exception thrown by an internal operation).
When set, the integrity of the stream may have been affected.
Multiple flags may be set by a single operation.

If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// istream::peek example
#include <iostream>     // std::cin, std::cout
#include <string>       // std::string

int main () {

  std::cout << "Please, enter a number or a word: ";
  char c = std::cin.peek();

  if ( (c >= '0') && (c <= '9') )
  {
    int n;
    std::cin >> n;
    std::cout << "You entered the number: " << n << '\n';
  }
  else
  {
    std::string str;
    std::getline (std::cin, str);
    std::cout << "You entered the word: " << str << '\n';
  }

  return 0;
}


Possible output:
Please, enter a number or a word: foobar
You entered the word: foobar

Data races

Modifies the stream object.
Concurrent access to the same stream object may cause data races, except for the standard stream object cin when this is synchronized with stdio (in this case, no data races are initiated, although no guarantees are given on the order in which read characters are attributed to threads).

Exception safety

Basic guarantee: if an exception is thrown, the object is in a valid state.
It throws an exception of member type failure if the resulting error state flag is not goodbit and member exceptions was set to throw for that state.
Any exception thrown by an internal operation is caught and handled by the function, setting badbit. If badbit was set on the last call to exceptions, the function rethrows the caught exception.

See also

<istream>