1 2 3 4 5 6 7 8 9

C++ Input and Output

*

*

*

*

The standard C++ library has a collection of classes that can be used for input and output
Most of these classes are based on a stream abstraction, the input or output device is viewed as a stream of characters
So far we have used two basic output streams, cout and cerr, these streams are objects of the class ostream
The
ostreamclass has a <<operator that is used to output a value onto the stream, the ostream class defines this operator for most of the standard C++ data types
As we have seen before, we can extend the range of this operator by defining our own << operator for any of the classes that we define For input we have one predefined object, cin, which is an object of the class istream
This class uses the >> operator to input a value, it defines the >> operator for most of the standard C++ data types
For each of the classes that we define, we can produce a version of the >> operator to input values of that class
The >> operator normally returns the istream that it was called on. If it can't read an item of the expect type it returns zero
This could happen if the end of file has been reached, or the next item on the input stream doesn't match the type of value that >> is expecting

*

*

*

*

*

*

23 March, 1998

Page 1