In the C++ programming language, input/output library refers to a family of class templates and supporting functions in the C++ Standard Library that implement stream-based input/output capabilities. It is an object-oriented alternative to C's -based streams from the C standard library.
Bjarne Stroustrup, the creator of C++, wrote the first version of the stream I/O library in 1984, as a type-safe and extensible alternative to C's I/O library. The library has undergone a number of enhancements since this early version, including the introduction of manipulators to control formatting, and templatization to allow its use with character types other than <code>char</code>.
Standardization in 1998 saw the library moved into the <code>std</code> namespace, and the main header changed from <code><iostream.h></code> to <code><iostream></code>. It is this standardized version that is covered in the rest of the article.
In C++20, the header <code><format></code> was added, adding <code>std::format()</code> and <code>std::formatter</code> classes. In C++23, the header <code><print></code> was added, which adds <code>std::print()</code> and <code>std::println()</code>, allowing for formatted printing to any output or file stream. These were both based on the existing fmtlib by Victor Zverovich.
Most of the classes in the library are actually very generalized class templates. Each template can operate on various character types, and even the operations themselves, such as how two characters are compared for equality, can be customized. However, the majority of code needs to do input and output operations using only one or two character types, thus most of the time the functionality is accessed through several typedefs, which specify names for commonly used combinations of template and character type.
For example, <code>basic_fstream<CharT, Traits></code> refers to the generic class template that implements input/output operations on file streams. It is usually used as <code>fstream</code> which is an alias for <code>basic_fstream<char,char_traits<char>></code>, or, in other words, <code>basic_fstream</code> working on characters of type <code>char</code> with the default character operation set.
The classes in the library could be divided into roughly two categories: abstractions and implementations. Classes, that fall into abstractions category, provide an interface which is sufficient for working with any type of a stream. The code using such classes doesn't depend on the exact location the data is read from or is written to. For example, such code could write data to a file, a memory buffer or a web socket without a recompilation. The implementation classes inherit the abstraction classes and provide an implementation for concrete type of data source or sink. The library provides implementations only for file-based streams and memory buffer-based streams.
The classes in the library could also be divided into two groups by whether it implements low-level or high-level operations. The classes that deal with low-level stuff are called stream buffers. They operate on characters without providing any formatting functionality. These classes are very rarely used directly. The high-level classes are called streams and provide various formatting capabilities. They are built on top of stream buffers.
The following table lists and categorizes all classes provided by the input-output library.
The classes of the input/output library reside in several headers.
<code><strstream></code> was used for <code>char[]</code> input/output devices and streams. It is superseded by using <code>std::stringstream</code> and the <code><spanstream></code> header, and was removed in C++26.
There are twelve stream buffer classes defined in the C++ language as the table.
<code>ios_base</code> and <code>basic_ios</code> are two classes that manage the lower-level bits of a stream. <code>ios_base</code> stores formatting information and the state of the stream. <code>basic_ios</code> manages the associated stream-buffer. <code>basic_ios</code> is commonly known as simply <code>ios</code> or <code>wios</code>, which are two typedefs for <code>basic_ios</code> with a specific character type. <code>basic_ios</code> and <code>ios_base</code> are very rarely used directly by programmers. Usually, their functionality is accessed through other classes such as <code>iostream</code> which inherit them.
C++ input/output streams are primarily defined by <code>iostream</code>, a header file that is part of the C++ standard library (the name stands for Input/Output Stream). In C++ and its predecessor, the C programming language, there is no special syntax for streaming data input or output. Instead, these are combined as a library of functions. Like the <code>cstdio</code> header inherited from C's stdio.h, <code>iostream</code> provides basic input and output services for C++ programs. iostream uses the objects <code>cin</code>, <code>cout</code>, <code>cerr</code>, and <code>clog</code> for sending data to and from the standard streams input, output, error (unbuffered), and log (buffered) respectively. As part of the C++ standard library, these objects are a part of the <code>std</code> namespace.
The <code>cout</code> object is of type <code>ostream</code>, which overloads the left bit-shift operator to make it perform an operation completely unrelated to bitwise operations, and notably evaluate to the value of the left argument, allowing multiple operations on the same ostream object, essentially as a different syntax for method cascading, exposing a fluent interface. The <code>cerr</code> and <code>clog</code> objects are also of type <code>ostream</code>, so they overload that operator as well. The <code>cin</code> object is of type <code>istream</code>, which overloads the right bit-shift operator. The directions of the bit-shift operators make it seem as though data is flowing towards the output stream or flowing away from the input stream.
Manipulators are objects that can modify a stream using the <code><<</code> or <code>>></code> operators.
Other manipulators can be found using the header <code>iomanip</code>.
The formatting manipulators must be "reset" at the end or the programmer will unexpectedly get their effects on the next output statement.
Some implementations of the C++ standard library have significant amounts of dead code. For example, GNU libstdc++ automatically constructs a locale when building an <code>ostream</code> even if a program never uses any types (date, time or money) that a locale affects, and a statically linked "Hello, World!" program that uses <code><iostream></code> of GNU libstdc++ produces an executable an order of magnitude larger than an equivalent program that uses <code><cstdio></code>. There exist partial implementations of the C++ standard library designed for space-constrained environments; their <code><iostream></code> may leave out features that programs in such environments may not need, such as locale support.
The pre-C++23 canonical "Hello, World!" program which used the <code><iostream></code> library, can be expressed as follows:
This program would output "Hello, world!" followed by a newline and standard output stream buffer flush.
The following example, which uses the <code><fstream></code> library, creates a file called and puts the text 'Hello, world!' followed by a newline into it.
Using the <code><print></code> library added in C++23 (which is also imported by the standard library module <code>std</code>), the post-C++23 canonical "Hello, World!" program is expressed as: