Standard Streams

Standard streams are preconnected input and output streams provided by the operating system. They are usually connected to the console or terminal where the program is running. The standard streams are:

  • cin - standard input stream
  • cout - standard output stream
  • cerr - standard error stream
  • clog - standard logging stream (buffered)

Class Hierarchy

   <ios>
   /   \

\ / // input/output streams | // file streams | // string streams

Formatting

operator<< and operator>> are overloaded for all built-in types. They provide formatted input and output.

Output: Formatting output is done by converting the value to a string and then writing the string to the stream. E.g.: cout << 42; converts the integer 42 to the string "42" and writes it to the standard output stream.

Input: Formatted input requires parsing the input string and converting it to the target type. E.g.: int i; cin >> i; reads a string from the standard input stream and converts it to an integer.

Error Bits

The ios class defines a set of error bits that can be set on a stream. These bits are used to indicate the state of the stream.

BitDescriptionFunction
goodbitNo errorgood()
eofbitEnd of fileeof()
failbitLogical error on i/o operationfail()
badbitRead/writing error on i/o operationbad()