Click on the banner to return to the class reference home page.

basic_istream


basic_istreambasic_iosios_base

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <istream>
template<class charT, class traits = char_traits<charT> >
class basic_istream 
: virtual public basic_ios<charT, traits>

Description

The class basic_istream defines a number of member function signatures that assist in reading and interpreting input from sequences controlled by a stream buffer.

Two groups of member function signatures share common properties: the formatted input functions (or extractors) and the unformatted input functions. Both groups of input functions obtain (or extract) input characters by calling basic_streambuf member functions. They both begin by constructing an object of class basic_istream::sentry and, if this object is in good state after construction, the function endeavors to obtain the requested input. The sentry object performs exception safe initialization, such as controlling the status of the stream or locking it in multithread environment.

Some formatted input functions parse characters extracted from the input sequence, converting the result to a value of some scalar data type, and storing the converted value in an object of that scalar type. The conversion behavior is locale dependent, and directly depend on the locale object imbued in the stream.

Interface

template<class charT, class traits = char_traits<charT> >
class basic_istream 
: virtual public basic_ios<charT, traits> {

public:

 typedef basic_istream<charT, traits>    istream_type;
 typedef basic_ios<charT, traits>        ios_type;
 typedef basic_streambuf<charT, traits>  streambuf_type;

 typedef traits                      traits_type;
 typedef charT                       char_type;
 typedef typename traits::int_type   int_type;
 typedef typename traits::pos_type   pos_type;
 typedef typename traits::off_type   off_type;
    
 explicit basic_istream(basic_streambuf<charT, traits> *sb);
 virtual ~basic_istream();

 class sentry 
  {
     public:

      inline explicit sentry(basic_istream<charT,traits>&,
                             bool noskipws = 0);
       ~sentry();
       operator bool ();
  };

 istream_type& operator>>(istream_type& (*pf)(istream_type&));
 istream_type& operator>>(ios_base& (*pf)(ios_base&));
 istream_type& operator>>(ios_type& (*pf)(ios_type&));

 istream_type& operator>>(bool& n);
 istream_type& operator>>(short& n);
 istream_type& operator>>(unsigned short& n);
 istream_type& operator>>(int& n);
 istream_type& operator>>(unsigned int& n);
 istream_type& operator>>(long& n);
 istream_type& operator>>(unsigned long& n);
 istream_type& operator>>(float& f);
 istream_type& operator>>(double& f);
 istream_type& operator>>(long double& f);
 
 istream_type& operator>>(void*& p);
     
 istream_type& operator>>(streambuf_type& sb);
 istream_type& operator>>(streambuf_type *sb);

 int_type get();
 
 istream_type& get(char_type *s, streamsize n, char_type delim);
 istream_type& get(char_type *s, streamsize n);
    
 istream_type& get(char_type& c);
    
 istream_type& get(streambuf_type& sb,char_type delim);
 istream_type& get(streambuf_type& sb);

 istream_type& getline(char_type *s, streamsize n,char_type delim);
 istream_type& getline(char_type *s, streamsize n);
 
 istream_type& ignore(streamsize n = 1, 
                      int_type delim = traits::eof());

 istream_type& read(char_type *s, streamsize n);

 streamsize readsome(char_type *s, streamsize n);
     
 int peek();
      
 pos_type tellg();

 istream_type& seekg(pos_type&);
 istream_type& seekg(off_type&, ios_base::seekdir);
     
 istream_type& putback(char_type c);
 istream_type& unget();

 streamsize gcount() const;

 int sync();
    
protected:

 explicit basic_istream( );

};

//global function

template<class charT, class traits>
basic_istream<charT, traits>&
ws(basic_istream<charT, traits>&);
template<class charT, class traits>
basic_istream<charT, traits>&
operator>> (basic_istream<charT, traits>&, charT&);
template<class charT, class traits>
basic_istream<charT, traits>&
operator>> (basic_istream<charT, traits>&, charT*);
template<class traits>
basic_istream<char, traits>&
operator>> (basic_istream<char, traits>&, unsigned char&);
template<class traits>
basic_istream<char, traits>&
operator>> (basic_istream<char, traits>&, signed char&);
template<class traits>
basic_istream<char, traits>&
operator>> (basic_istream<char, traits>&, unsigned char*);
template<class traits>
basic_istream<char, traits>&
operator>> (basic_istream<char, traits>&, signed char*);

Types

char_type
int_type
ios_type
istream
istream_type
off_type
pos_type
streambuf_type
traits_type
wistream

Public Constructor

explicit basic_istream(basic_streambuf<charT, traits>* sb);

Public Destructor

virtual ~basic_istream();

Sentry Class

explicit sentry(basic_istream<charT,traits>&, bool noskipws=0);
~sentry();
operator bool();

Extractors

istream_type& 
operator>>(istream_type&
          (*pf) (istream_type&));
istream_type& 
operator>>(ios_type& (*pf) (ios_type&));
istream_type& 
operator>>(ios_base& (*pf) (ios_base&));
istream_type& 
operator>>(bool& n);
istream_type& 
operator>>(short& n);
istream_type& 
operator>>(unsigned short& n);
istream_type& 
operator>>(int& n);
istream_type& 
operator>>(unsigned int& n);
istream_type& 
operator>>(long& n);
istream_type& 
operator>>(unsigned long& n);
istream_type& 
operator>>(float& f);
istream_type&
operator>>(double& f);
istream_type&
operator>>(long double& f);
istream_type&
operator>>(void*& p);
istream_type&
operator>>(streambuf_type* sb);
istream_type&
operator>>(streambuf_type& sb);

Unformatted Functions

streamsize 
gcount() const;
int_type 
get();
istream_type& 
get(char_type& c);
istream_type& 
get(char_type* s, streamsize n,char_type delim);
istream_type& 
get(char_type* s, streamsize n);
istream_type& 
get(streambuf_type& sb,char_type delim);
istream_type& 
get(streambuf_type& sb);
istream_type& 
getline(char_type* s, streamsize n, char_type delim);
istream_type& 
getline(char_type* s, streamsize n);
istream_type& 
ignore(streamsize n=1, int_type delim=traits::eof());
int_type 
peek();
istream_type& 
putback(char_type c);
istream_type& 
read(char_type* s, streamsize n);
streamsize 
readsome(char_type* s, streamsize n);
istream_type& 
seekg(pos_type& pos);
istream_type& 
seekg(off_type& off, ios_base::seekdir dir);
int 
sync();
pos_type 
tellg();
istream_type& 
unget();

Non Member Functions

template<class charT, class traits>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is, charT& c);
template<class charT, class traits>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is, charT* s);
Template<class traits>
basic_istream<char, traits>&
operator>>(basic_istream<char, traits>& is, unsigned char& c);
Template<class traits>
basic_istream<char, traits>&
operator>>(basic_istream<char, traits>& is, signed char& c);
Template<class traits>
basic_istream<char, traits>&
operator>>(basic_istream<char, traits>& is, unsigned char* c);
Template<class traits>
basic_istream<char, traits>&
operator>>(basic_istream<char, traits>& is, signed char* c);
template<class charT, class traits>
basic_istream<charT, traits>&
ws(basic_istream<charT, traits>& is);

Examples

//
// stdlib/examples/manual/istream1.cpp
//
#include<iostream>
#include<istream>
#include<fstream>

void main ( )
{
  using namespace std;

  float f= 3.14159;
  int   i= 3;
  char  s[200];

  // open a file for read and write operations 
  ofstream out("example", ios_base::in | ios_base::out
               | ios_base::trunc);

  // tie the istream object to the ofstream filebuf 
  istream  in (out.rdbuf());

  // output to the file
  out << "Annie is the Queen of porting" << endl;
  out << f << endl;
  out << i << endl;
  
  // seek to the beginning of the file
  in.seekg(0);
  
  f = i = 0;

  // read from the file using formatted functions
  in >> s >> f >> i;

  // seek to the beginning of the file
  in.seekg(0,ios_base::beg);

  // output the all file to the standard output
  cout << in.rdbuf();

  // seek to the beginning of the file
  in.seekg(0);
 
  // read the first line in the file
  // "Annie is the Queen of porting"
  in.getline(s,100);

  cout << s << endl;

  // read the second line in the file
  // 3.14159
  in.getline(s,100);

  cout << s << endl;

  // seek to the beginning of the file
  in.seekg(0);

  // read the first line in the file
  // "Annie is the Queen of porting"
  in.get(s,100);

  // remove the newline character
  in.ignore();

  cout << s << endl;

  // read the second line in the file
  // 3.14159
  in.get(s,100);

  cout << s << endl;

  // remove the newline character
  in.ignore();

  // store the current file position   
  istream::pos_type position = in.tellg();
 
  out << "replace the int" << endl;

  // move back to the previous saved position
  in.seekg(position);   

  // output the remain of the file
  // "replace the int"
  // this is equivalent to
  // cout << in.rdbuf();
  while( !char_traits<char>::eq_int_type(in.peek(), char_traits<char>::eof()) )
   cout << char_traits<char>::to_char_type(in.get()); 
 
  cout << "\n\n\n" << flush;
}

//
// istream example two
//
#include <iostream>

void main ( )
{
  using namespace std;

  char p[50];

  // remove all the white spaces  
  cin >> ws;

  // read characters from stdin until a newline
  // or 49 characters have been read
  cin.getline(p,50);

  // output the result to stdout
  cout << p;   
}

See Also

char_traits(3C++), ios_base(3C++), basic_ios(3C++), basic_streambuf(3C++), basic_iostream(3C++)

Working Paper for Draft Proposed International Standard for Information Systems--Programming Language C++, Section 27.6.1

Standards Conformance

ANSI X3J16/ISO WG21 Joint C++ Committee


©Copyright 1996, Rogue Wave Software, Inc.