cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : IOstream Library : istream : peek
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
IOstream Library
manipulators
classes:
· filebuf
· fstream
· ifstream
· ios
· iostream
· ios_base
· istream
· istringstream
· ofstream
· ostream
· ostringstream
· streambuf
· stringbuf
· stringstream
objects:
· cerr
· cin
· clog
· cout
types:
· fpos
· streamoff
· streampos
· streamsize
istream
· istream::istream
· istream::~istream
member classes:
· istream::sentry
member functions:
· istream::gcount
· istream::get
· istream::getline
· istream::ignore
· istream::operator>>
· istream::peek
· istream::putback
· istream::read
· istream::readsome
· istream::seekg
· istream::sync
· istream::tellg
· istream::unget

-

istream::peek public member function
int peek ( );

Peek next character

Reads and returns the next character without extracting it, i.e. leaving it as the next character to be extracted from the stream.

Parameters

none

Return Value

The value of the next character.
In the case of error, the function returns EOF (or traits::eof() for other traits) and modifies the state flags accordingly:

flagerror
eofbitThe end of the source of characters is reached during its operations.
failbit-
badbitAn error other than the above happened.

Additionaly, in any of these cases, if the appropriate flag has been set with member function ios::exceptions, an exception of type ios_base::failure is thrown.

Example

// istream peek
#include <iostream>
using namespace std;

int main () {
  char c;
  int n;
  char str[256];

  cout << "Enter a number or a word: ";
  c=cin.peek();

  if ( (c >= '0') && (c <= '9') )
  {
    cin >> n;
    cout << "You have entered number " << n << endl;
  }
  else
  {
    cin >> str;
    cout << " You have entered word " << str << endl;
  }

  return 0;
}

Basic template member declaration

( basic_istream<charT,traits> )
typedef traits::int_type int_type;
int_type peek ( );

See also

istream::get Get unformatted data from stream (public member function)
istream::operator>> Extract formatted data (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us