Input/output library - cppreference.com (original) (raw)

C++ includes the following input/output libraries: an OOP-style stream-based I/O library, print-based family of functions(since C++23), and the standard set of C-style I/O functions.

Contents

[edit] Stream-based I/O

The stream-based input/output library is organized around abstract input/output devices. These abstract devices allow the same code to handle input/output to files, memory streams, or custom adaptor devices that perform arbitrary operations (e.g. compression) on the fly.

Most of the classes are templated, so they can be adapted to any basic character type. Separate typedefs are provided for the most common basic character types (char and wchar_t). The classes are organized into the following hierarchy:

std-io-complete-inheritance.svg

Inheritance diagram

Abstraction
Defined in header
ios_base manages formatting flags and input/output exceptions (class) [edit]
basic_ios manages an arbitrary stream buffer (class template) [edit]
Defined in header
basic_streambuf abstracts a raw device (class template) [edit]
Defined in header
basic_ostream wraps a given abstract device (std::basic_streambuf)and provides high-level output interface (class template) [edit]
Defined in header
basic_istream wraps a given abstract device (std::basic_streambuf)and provides high-level input interface (class template) [edit]
basic_iostream wraps a given abstract device (std::basic_streambuf)and provides high-level input/output interface (class template) [edit]
File I/O implementation
Defined in header
basic_filebuf implements raw file device (class template) [edit]
basic_ifstream implements high-level file stream input operations (class template) [edit]
basic_ofstream implements high-level file stream output operations (class template) [edit]
basic_fstream implements high-level file stream input/output operations (class template) [edit]
String I/O implementation
Defined in header
basic_stringbuf implements raw string device (class template) [edit]
basic_istringstream implements high-level string stream input operations (class template) [edit]
basic_ostringstream implements high-level string stream output operations (class template) [edit]
basic_stringstream implements high-level string stream input/output operations (class template) [edit]
Array I/O implementations
Defined in header
basic_spanbuf(C++23) implements raw fixed character buffer device (class template) [edit]
basic_ispanstream(C++23) implements fixed character buffer input operations (class template) [edit]
basic_ospanstream(C++23) implements fixed character buffer output operations (class template) [edit]
basic_spanstream(C++23) implements fixed character buffer input/output operations (class template) [edit]
Defined in header
strstreambuf(deprecated in C++98)(removed in C++26) implements raw character array device (class) [edit]
istrstream(deprecated in C++98)(removed in C++26) implements character array input operations (class) [edit]
ostrstream(deprecated in C++98)(removed in C++26) implements character array output operations (class) [edit]
strstream(deprecated in C++98)(removed in C++26) implements character array input/output operations (class) [edit]
Synchronized output (since C++20)
Defined in header
basic_syncbuf(C++20) synchronized output device wrapper (class template) [edit]
basic_osyncstream(C++20) synchronized output stream wrapper (class template) [edit]

[edit] Typedefs

The following typedefs for common character types are provided in namespace std:

Type Definition
Defined in header
std::ios std::basic_ios<char>
std::wios std::basic_ios<wchar_t>
Defined in header
std::streambuf std::basic_streambuf<char>
std::wstreambuf std::basic_streambuf<wchar_t>
Defined in header
std::istream std::basic_istream<char>
std::wistream std::basic_istream<wchar_t>
std::iostream std::basic_iostream<char>
std::wiostream std::basic_iostream<wchar_t>
Defined in header
std::ostream std::basic_ostream<char>
std::wostream std::basic_ostream<wchar_t>
Defined in header
std::filebuf std::basic_filebuf<char>
std::wfilebuf std::basic_filebuf<wchar_t>
std::ifstream std::basic_ifstream<char>
std::wifstream std::basic_ifstream<wchar_t>
std::ofstream std::basic_ofstream<char>
std::wofstream std::basic_ofstream<wchar_t>
std::fstream std::basic_fstream<char>
std::wfstream std::basic_fstream<wchar_t>
Defined in header
std::stringbuf std::basic_stringbuf<char>
std::wstringbuf std::basic_stringbuf<wchar_t>
std::istringstream std::basic_istringstream<char>
std::wistringstream std::basic_istringstream<wchar_t>
std::ostringstream std::basic_ostringstream<char>
std::wostringstream std::basic_ostringstream<wchar_t>
std::stringstream std::basic_stringstream<char>
std::wstringstream std::basic_stringstream<wchar_t>
Defined in header
std::spanbuf (C++23) std::basic_spanbuf<char>
std::wspanbuf (C++23) std::basic_spanbuf<wchar_t>
std::ispanstream (C++23) std::basic_ispanstream<char>
std::wispanstream (C++23) std::basic_ispanstream<wchar_t>
std::ospanstream (C++23) std::basic_ospanstream<char>
std::wospanstream (C++23) std::basic_ospanstream<wchar_t>
std::spanstream (C++23) std::basic_spanstream<char>
std::wspanstream (C++23) std::basic_spanstream<wchar_t>
Defined in header
std::syncbuf (C++20) std::basic_syncbuf<char>
std::wsyncbuf (C++20) std::basic_syncbuf<wchar_t>
std::osyncstream (C++20) std::basic_osyncstream<char>
std::wosyncstream (C++20) std::basic_osyncstream<wchar_t>

[edit] Predefined standard stream objects

[edit] I/O Manipulators

The stream-based I/O library uses I/O manipulators (e.g. std::boolalpha, std::hex, etc.) to control how streams behave.

[edit] Types

The following auxiliary types are defined:

Defined in header

The following typedef names for std::fpos<std::mbstate_t> are provided:

Defined in header
Type Definition
std::streampos std::fpos<std::char_traits<char>::state_type>
std::wstreampos std::fpos<std::char_traits<wchar_t>::state_type>
std::u8streampos (C++20) std::fpos<std::char_traits<char8_t>::state_type>
std::u16streampos (C++11) std::fpos<std::char_traits<char16_t>::state_type>
std::u32streampos (C++11) std::fpos<std::char_traits<char32_t>::state_type>

[edit] Error category interface (since C++11)

[edit] Print functions (since C++23)

The Unicode-aware print-family functions that perform formatted I/O on text that is already formatted. They bring all the performance benefits of std::format, are locale-independent by default, reduce global state, avoid allocating a temporary std::string object and calling operator<<, and in general make formatting more efficient compared to iostreams and stdio.

The following print-like functions are provided:

Defined in header
print(C++23) prints to stdout or a file stream using formatted representation of the arguments (function template) [edit]
println(C++23) same as std::print except that each print is terminated by additional new line (function template) [edit]
vprint_unicodevprint_unicode_buffered(C++23) prints to Unicode capable stdout or a file stream using type-erased argument representation (function) [edit]
vprint_nonunicodevprint_nonunicode_buffered(C++23) prints to stdout or a file stream using type-erased argument representation (function) [edit]
Defined in header
print(std::ostream)(C++23) outputs formatted representation of the arguments (function template) [edit]
println(std::ostream)(C++23) outputs formatted representation of the arguments with appended '\n' (function template) [edit]

[edit] C-style I/O

C++ also includes the input/output functions defined by C, such as std::fopen, std::getc, etc.

[edit] See also