The C++ iostream library is an object-oriented library that provides input and output functionality using streams. The iostreams classes support type-safe I/O of built-in types and can be extended to support user-defined types by overloading the >> and << operators.
Use this tag for questions about using iostreams, including writing overloaded operators for your own types.
The C++ standard library defines the std::istream
, std::ostream
and std::iostream
base classes, as well as the standard stream objects std::cin
, std::cout
and std::cerr
and derived iostream types for reading/writing files and strings. An iostream object is responsible for formatting operations (such as converting an integer 1234
into the string "1234"
or vice versa) but uses a stream buffer (an object derived from std::streambuf
) to interface with the underlying data stream. The stream buffer does any buffering of characters, manages the stream position and transports characters to/from an external device such as a file.
Iostreams use locales to support internationalization and are implemented as a hierarchy of class templates to support different types of characters, so that std::istream
is actually a typedef for the specialization std::basic_istream<char, std::char_traits<char>>
. The first template parameter is the character type used by the stream and the second is a traits class that provides operations for working with the character type.
A quick introduction to iostreams can be found in Chapter 3: A tour of the Standard Library in Stroustrup's TC++PL. Josuttis's The C++ Standard Library gives more information. The most detailed reference on using and extending iostreams is Langer & Kreft's Standard C++ IOStreams and Locales (see excerpt about stream buffers.)