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.ISO/IEC 14882:2003 Programming Languages – C++. [lib.string.streams]/1 It is an object-oriented alternative to C's -based streams from the C standard library.
History Bjarne Stroustrup, the creator of C++, wrote the first version of the stream I/O library in 1984, <stream.h>, as a type-safe and extensible alternative to C's I/O library, for his C with Classes language which would eventually evolve into C++. The API overloaded the << and >> operators to indicate output and input.
Standardization in 1998 saw the library moved into the std namespace, and the main header changed from <iostream.h> to <iostream>.
In C++20, the header <format> was added, adding std::format() and std::formatter classes. In C++23, the header <print> was added, which adds std::print() and std::println(), allowing for formatted printing to any output or file stream. These were both based on the existing fmtlib by Victor Zverovich. The API is similar to the C printf function (actually it matches Python's format) and is proving to be more popular as the code is more readable.
Stream-based I/O library The "Hello, World!" program using the features of the <iostream> header can be expressed as follows:
import std;
int main() { std::cout << "Hello, world!" << std::endl; } This program would output "" followed by a newline and standard output stream buffer flush.
The following example, which uses the features of the <fstream> header, creates a file called '' and puts the text '' followed by a newline into it.
import std;
using std::ofstream;
int main() { ofstream file("file.txt"); file << "Hello, world!" << std::endl; }
Headers The classes of the input/output library reside in several headers. <ios> contains the definitions of std::ios_base and std::basic_ios classes, that manage formatting information and the associated stream-buffer. <istream> contains the definition of std::basic_istream class template, which implements formatted input. <ostream> contains the definition of std::basic_ostream class template, which implements formatted output. <iostream> contains the definition of std::basic_iostream class template, which implements formatted input and output, and includes <ios>, <istream> and <ostream>. <fstream> contains the definitions of std::basic_ifstream, std::basic_ofstream and std::basic_fstream class templates which implement formatted input, output and input/output on file streams. <sstream> contains the definitions of std::basic_istringstream, std::basic_ostringstream and std::basic_stringstream class templates which implement formatted input, output and input/output on string-based streams. <iomanip> contains formatting manipulators. <iosfwd> contains forward declarations of all classes in the input/output library. <spanstream> provides improved input/output devices and streams for char[]. These use a std::span<CharT> to the underlying buffer. This header supersedes the former <strstream>. <syncstream> provides synchronised output devices and streams.
<strstream> was used for char[] input/output devices and streams. It is superseded by using std::stringstream and the <spanstream> header, and was removed in C++26.
Classes Most of the classes in the stream-based I/O 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, basic_fstream<CharT, Traits> refers to the generic class template that implements input/output operations on file streams. It is usually used as fstream which is an alias for basic_fstream<char, char_traits<char>>, or, in other words, basic_fstream working on characters of type char 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.
Class Explanation Typedefs Stream buffers (low level functionality) basic_streambuf provides abstract low level input/output interface, that can be implemented for concrete data sources or sinks. Rarely used directly.streambuf – operates on characters of type char wstreambuf – operates on characters of type wchar_t basic_filebuf implements low level input/output interface for file-based streams. Rarely used directly.filebuf – operates on characters of type char wfilebuf – operates on characters of type wchar_t basic_stringbuf implements low level input/output interface for string-based streams. Rarely used directly.stringbuf – operates on characters of type char wstringbuf – operates on characters of type wchar_t Support classes ios_base manages formatting information and exception state basic_ios manages a stream bufferios – operates on characters of type char wios – operates on characters of type wchar_t Input streams buffers (high level functionality) basic_istream wraps an abstract stream buffer and provides high level input interface, such as formatting capabilities.istream – operates on characters of type char wistream – operates on characters of type wchar_t basic_ifstream an input stream that wraps a file stream buffer. Provides functions to open or close a file in addition to those of generic input streamifstream – operates on characters of type char wifstream – operates on characters of type wchar_t basic_istringstream an input stream that wraps a string stream buffer. Provides functions to access the underlying string in addition to those of generic input streamistringstream – operates on characters of type char wistringstream – operates on characters of type wchar_t Output streams buffers (high level functionality) basic_ostream wraps an abstract stream buffer and provides high level output interface, such as formatting capabilities.ostream – operates on characters of type char wostream – operates on characters of type wchar_t basic_ofstream an output stream that wraps a file stream buffer. Provides functions to open or close a file in addition to those of generic output streamofstream – operates on characters of type char wofstream – operates on characters of type wchar_t basic_ostringstream an output stream that wraps a string stream buffer. Provides functions to access the underlying string in addition to those of generic output streamostringstream – operates on characters of type char wostringstream – operates on characters of type wchar_t Input/output streams buffers (high level functionality) basic_iostream wraps an abstract stream buffer and provides high level input/output interface, such as formatting capabilities.iostream – operates on characters of type char wiostream – operates on characters of type wchar_t basic_fstream an input/output stream that wraps a file stream buffer. Provides functions to open or close a file in addition to those of generic input/output streamfstream – operates on characters of type char wfstream – operates on characters of type wchar_t basic_stringstream an input/output stream that wraps a string stream buffer. Provides functions to access the underlying string in addition to those of generic input/output streamstringstream – operates on characters of type char wstringstream – operates on characters of type wchar_t
Manipulators
Manipulators are objects that can modify a stream using the << or >> operators.
+ Name Description boolalpha / noboolalpha whether variables of type bool appear as true and false or as 0 and 1 in the stream. skipws / noskipws whether the white space is skipped in input operations showbase / noshowbase whether the notational base of the number is displayed showpoint / noshowpoint whether to display the fractional part of a floating point number, when the fractional part is zero showpos / noshowpos whether to display + before positive numbers unitbuf / nounitbuf whether the output should be buffered uppercase / nouppercase whether uppercase characters should be used in hexadecimal integer and floating-point output left / right / internal how a number should be justified dec / oct / hex the notation an integer number should be displayed in fixed / scientific/ hexfloat / defaultfloat the notation a floating-point number should be displayed in setw(int n) minimum number of CharT for next object setfill(CharT c) character used to get the minimum number of CharT setprecision(int n) sets the number of significant digits for floating-point numbersendl "end line": inserts a newline into the stream and calls flush.ends "end string": inserts a null character into the stream and calls flush.flush forces an output stream to write any buffered charactersws causes an inputstream to 'eat' whitespaceshowpoint tells the stream to show the decimal point and some zeros with whole numbers
Other manipulators can be found using the <iomanip> header.
Input/output streams
C++ input/output streams are primarily defined in <iostream> (for "input/output stream"), a header file that is part of the C++ standard library. 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 <cstdio> header inherited from C's <stdio.h> (C's I/O library), <iostream> provides basic input and output services for C++ programs. iostream uses the objects std::cin, std::cout, std::cerr, and std::clog 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 std namespace.
The <iostream> objects are global objects with static duration tied to a static std::ios_base::Init instance (responsible for their construction and destruction)., which may be safely concurrently accessed from multiple threads for formatted and unformatted output (unless std::ios_base::sync_with_stdio(false) has been used). +ObjectTypeDescriptionstd::cinstd::istreamReads from the stdin input streamstd::wcinstd::wistreamstd::coutstd::ostreamWrites to the stdout output streamstd::wcoutstd::wostreamstd::cerrstd::ostreamWrites to the stderr output stream, unbufferedstd::wcerrstd::wostreamstd::clogstd::ostreamWrites to the stderr output stream, buffered (intended for logging)std::wclogstd::wostream