basic_istream extractor operator >>

Extracts characters or sequences of characters and converts if necessary to numerical data.

  basic_istream<charT, traits>& operator >>

  (basic_istream<charT, traits>& (*pf)

  (basic_istream<charT,traits>&))  

Returns pf(*this).

  basic_istream<charT, traits>& operator >>
 (basic_ios<charT, traits>& (*pf)(basic_ios<charT,traits>&))
  

Calls pf(*this) then returns *this.

basic_istream<charT, traits>& operator >>(char_type *s);

Extracts a char array and stores it in s if possible otherwise calls setstate(failbit). If width() is set greater than zero width()-1, elements are extracted; otherwise up to size of s-1 elements are extracted. Scan stops with a whitespace "as if" in fscanf().

basic_istream<charT, traits>& operator >>(char_type& c);

Extracts a single character and stores it in c if possible, otherwise calls setstate(failbit).

basic_istream<charT, traits>& operator >>(void*& p);

Converts a pointer to void and stores it in p.

  basic_istream<charT, traits>& operator >>
 (basic_streambuf<char_type, traits>* sb);
  

Extracts a basic_streambuf type and stores it in sb if possible, otherwise calls setstate(failbit).

Remarks

The various overloaded extractors are used to obtain formatted input dependent upon the type of argument. Since they return a reference to the calling stream they may be chained in a series of extractions. The overloaded extractors work "as if" like fscanf() in standard C and read until a white space character or EOF is encountered.

The white space character is not extracted and is not discarded, but simply ignored. Be careful when mixing unformatted input operations with the formatted extractor operators, such as when using console input.

The this pointer is returned.

See Also

basic_ostream::operator

Listing: Example of basic_istream:: extractor usage:
// The ewl-test input file contains
// float 33.33 double 3.16e+10 Integer 789 character C

#include <iostream>
#include <fstream>
#include <cstdlib>

char ioFile[81] = "ewl-test";

int main()
{ 
using namespace std;
   ifstream in(ioFile);
   if(!in.is_open())
   {cout << "cannot open file for input"; exit(1);}
   char type[20];
   double d;
   int i;
   char ch;

   in    >> type >> d;
   cout << type << " " << d << endl;

   in    >> type >> d;
   cout << type << " " << d << endl;

   in    >> type >> i;
   cout << type << " " << i << endl;

   in    >> type >> ch;
   cout << type << " " << ch << endl;
   cout << "\nEnter an integer: ";

   cin >> i;
   cout << "Enter a word: ";

   cin >> type;
   cout << "Enter a character \ "
      << "then a space then a double: ";

   cin >> ch >> d;
   cout << i << " " << type << " " 
     << ch << " " << d << endl;

   in.close();
   return 0;
}

Result:

  float 33.33

  double 3.16e+10

  Integer 789

  character C

  Enter an integer: 123 <enter>

  Enter a word: CodeWarrior <enter>

  Enter a character then a space then a double: a 12.34 <enter>

  123 CodeWarrior a 12.34