09 Streams
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 streamcout- standard output streamcerr- standard error streamclog- standard logging stream (buffered)
Class Hierarchy
<ios>
/ \
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.
| Bit | Description | Function |
|---|---|---|
goodbit | No error | good() |
eofbit | End of file | eof() |
failbit | Logical error on i/o operation | fail() |
badbit | Read/writing error on i/o operation | bad() |