IO Overview CSCE 121 Strongly influenced by slides created by Bjarne Stroustrup and Jennifer Welch
cin & cout Standard in Standard out Standard error Where input comes into a program by default Normally the keyboard Can be modified (e.g. pipes in UNIX) Standard out Where output is sent Normally the screen Can be modified (e.g. pipes) Standard error Where error messages are sent Frequently set to a file cin cout cerr clog Standard in Standard out Standard err buffered error Keyboard Monitor Default: Same as cout Default: Same as cerr Apparently, clog is the same as cerr, just buffered. Example: $ ./prgm [ARGS] 0<IN 1>OUT 2>ERR
Input and Output (IO or I/O) Program Output
Input and Output (IO or I/O) data source: input library input device device driver our program data destination: output library device driver output device
The Stream Model An ostream Values of various types ‘c’ “somewhere” ostream 1.234 Values of various types 123 buffer An ostream turns values of various types into character sequences sends those characters somewhere (console, file, main memory, another computer…)
The Stream Model An istream Values of various types ‘c’ “somewhere” istream 1.234 Values of various types 123 buffer An istream gets characters from somewhere (console, file, main memory, another computer,…) turns character sequences into values of various types
The Stream Model Supports reading and writing of typed entities << (output) and >> (input/extraction) plus other operations type-safe formatted typically stored as text but not necessarily, e.g, binary streams extensible define your own I/O operations for your own types a stream can be attached to any I/O or storage device
Many different Streams What applies to cout/cin will also apply to file streams string streams Etc.
Model http://www.cplusplus.com/reference/iolibrary/