Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io"

From cppreference.com
< cpp
(more hierarchical organization, rewording)
m (See also: +Filesystem library)
 
(33 intermediate revisions by 19 users not shown)
Line 1: Line 1:
 
{{title|Input/output library}}
 
{{title|Input/output library}}
{{cpp/io/sidebar}}
+
{{cpp/io/navbar}}
  
C++ includes two input/output libraries: a modern, stream-based I/O library and the standard set of C-style I/O functions.
+
C++ includes the following input/output libraries: an [[enwiki:Object-oriented_programming|OOP-style]] [[#Stream-based I/O|stream-based I/O]] library{{rev inl|since=c++23|, [[#Print functions (since C++23)|print-based family]] of functions}}, and the standard set of [[#C-style I/O|C-style I/O]] functions.
  
 
===Stream-based I/O===
 
===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.
 
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 ({{c|char}} and {{c|wchar_t}}). The classes are organized into the following hierarchy:
+
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 ({{c/core|char}} and {{c/core|wchar_t}}). The classes are organized into the following hierarchy:
  
{{inheritance diagram|image=std-io-complete-inheritance.svg|map=
+
{{inheritance diagram/std-io-complete}}
rect 215 4 318 36 [[cpp/io/ios_base]]
+
rect 206 69 327 107 [[cpp/io/basic_ios]]
+
rect 292 139 413 177 [[cpp/io/basic_istream]]
+
rect 120 139 241 177 [[cpp/io/basic_ostream]]
+
rect 395 280 515 318 [[cpp/io/basic_ifstream]]
+
rect 383 350 527 388 [[cpp/io/basic_istringstream]]
+
rect 206 209 327 247 [[cpp/io/basic_iostream]]
+
rect 17 280 138 318 [[cpp/io/basic_ofstream]]
+
rect 4 350 152 388 [[cpp/io/basic_ostringstream]]
+
rect 189 280 310 318 [[cpp/io/basic_fstream]]
+
rect 179 350 320 388 [[cpp/io/basic_stringstream]]
+
}}
+
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list h2 | Abstraction}}
+
{{dsc h2|Abstraction}}
{{dcl list class | cpp/io/ios_base | manages formatting flags and input/output exceptions}}
+
{{dsc header|ios}}
{{dcl list tclass | cpp/io/basic_ios | manages an arbitrary stream buffer}}
+
{{dsc inc|cpp/io/dsc ios_base}}
{{dcl list tclass | cpp/io/basic_streambuf | abstracts a raw device}}
+
{{dsc inc|cpp/io/dsc basic_ios}}
{{dcl list tclass | cpp/io/basic_istream | wraps a given abstract device ({{c|std::basic_streambuf}})<br/> and provides high-level input interface}}
+
{{dsc header|streambuf}}
{{dcl list tclass | cpp/io/basic_ostream | wraps a given abstract device ({{c|std::basic_streambuf}})<br/> and provides high-level output interface}}
+
{{dsc inc|cpp/io/dsc basic_streambuf}}
{{dcl list tclass | cpp/io/basic_iostream | wraps a given abstract device ({{c|std::basic_streambuf}})<br/> and provides high-level input/output interface}}
+
{{dsc header|ostream}}
{{dcl list h2 | File I/0 implementation}}
+
{{dsc inc|cpp/io/dsc basic_ostream}}
{{dcl list tclass | cpp/io/basic_filebuf | implements raw file device}}
+
{{dsc header|istream}}
{{dcl list tclass | cpp/io/basic_ifstream | implements high-level file stream input operations}}
+
{{dsc inc|cpp/io/dsc basic_istream}}
{{dcl list tclass | cpp/io/basic_ofstream | implements high-level file stream output operations}}
+
{{dsc inc|cpp/io/dsc basic_iostream}}
{{dcl list tclass | cpp/io/basic_fstream | implements high-level file stream input/output operations}}
+
{{dsc h2|File I/O implementation}}
{{dcl list h2 | String I/0 implementation}}
+
{{dsc header|fstream}}
{{dcl list tclass | cpp/io/basic_stringbuf | implements raw string device}}
+
{{dsc inc|cpp/io/dsc basic_filebuf}}
{{dcl list tclass | cpp/io/basic_istringstream | implements high-level string stream input operations}}
+
{{dsc inc|cpp/io/dsc basic_ifstream}}
{{dcl list tclass | cpp/io/basic_ostringstream | implements high-level string stream output operations}}
+
{{dsc inc|cpp/io/dsc basic_ofstream}}
{{dcl list tclass | cpp/io/basic_stringstream | implements high-level string stream input/output operations}}
+
{{dsc inc|cpp/io/dsc basic_fstream}}
{{dcl list h2 | Array I/O implementations}}
+
{{dsc h2|String I/O implementation}}
{{dcl list class | cpp/io/strstreambuf | notes={{mark deprecated}} | implements raw character array device}}
+
{{dsc header|sstream}}
{{dcl list class | cpp/io/istrstream | notes={{mark deprecated}} | implements character array input operations}}
+
{{dsc inc|cpp/io/dsc basic_stringbuf}}
{{dcl list class | cpp/io/ostrstream | notes={{mark deprecated}} | implements character array output operations}}
+
{{dsc inc|cpp/io/dsc basic_istringstream}}
{{dcl list class | cpp/io/strstream | notes={{mark deprecated}} | implements character array input/output operations}}
+
{{dsc inc|cpp/io/dsc basic_ostringstream}}
{{dcl list end}}
+
{{dsc inc|cpp/io/dsc basic_stringstream}}
 +
{{dsc h2|Array I/O implementations}}
 +
{{dsc header|spanstream}}
 +
{{dsc inc|cpp/io/dsc basic_spanbuf}}
 +
{{dsc inc|cpp/io/dsc basic_ispanstream}}
 +
{{dsc inc|cpp/io/dsc basic_ospanstream}}
 +
{{dsc inc|cpp/io/dsc basic_spanstream}}
 +
{{dsc header|strstream}}
 +
{{dsc inc|cpp/io/dsc strstreambuf}}
 +
{{dsc inc|cpp/io/dsc istrstream}}
 +
{{dsc inc|cpp/io/dsc ostrstream}}
 +
{{dsc inc|cpp/io/dsc strstream}}
 +
{{dsc h2|Synchronized output}}
 +
{{dsc header|syncstream}}
 +
{{dsc inc|cpp/io/dsc basic_syncbuf}}
 +
{{dsc inc|cpp/io/dsc basic_osyncstream}}
 +
{{dsc end}}
  
 
====Typedefs====
 
====Typedefs====
 +
The following typedefs for common character types are provided in namespace {{tt|std}}:
 +
{{dsc begin}}
 +
{{dsc hitem|Type|Definition}}
 +
{{dsc header|ios}}
 +
{{dsc|{{lc|std::ios}}|{{co|std::basic_ios<char>}}}}
 +
{{dsc|{{lc|std::wios}}|{{co|std::basic_ios<wchar_t>}}}}
 +
{{dsc header|streambuf}}
 +
{{dsc|{{lc|std::streambuf}}|{{co|std::basic_streambuf<char>}}}}
 +
{{dsc|{{lc|std::wstreambuf}}|{{co|std::basic_streambuf<wchar_t>}}}}
 +
{{dsc header|istream}}
 +
{{dsc|{{lc|std::istream}}|{{co|std::basic_istream<char>}}}}
 +
{{dsc|{{lc|std::wistream}}|{{co|std::basic_istream<wchar_t>}}}}
 +
{{dsc|{{lc|std::iostream}}|{{co|std::basic_iostream<char>}}}}
 +
{{dsc|{{lc|std::wiostream}}|{{co|std::basic_iostream<wchar_t>}}}}
 +
{{dsc header|ostream}}
 +
{{dsc|{{lc|std::ostream}}|{{co|std::basic_ostream<char>}}}}
 +
{{dsc|{{lc|std::wostream}}|{{co|std::basic_ostream<wchar_t>}}}}
 +
{{dsc header|fstream}}
 +
{{dsc|{{lc|std::filebuf}}|{{co|std::basic_filebuf<char>}}}}
 +
{{dsc|{{lc|std::wfilebuf}}|{{co|std::basic_filebuf<wchar_t>}}}}
 +
{{dsc|{{lc|std::ifstream}}|{{co|std::basic_ifstream<char>}}}}
 +
{{dsc|{{lc|std::wifstream}}|{{co|std::basic_ifstream<wchar_t>}}}}
 +
{{dsc|{{lc|std::ofstream}}|{{co|std::basic_ofstream<char>}}}}
 +
{{dsc|{{lc|std::wofstream}}|{{co|std::basic_ofstream<wchar_t>}}}}
 +
{{dsc|{{lc|std::fstream}}|{{co|std::basic_fstream<char>}}}}
 +
{{dsc|{{lc|std::wfstream}}|{{co|std::basic_fstream<wchar_t>}}}}
 +
{{dsc header|sstream}}
 +
{{dsc|{{lc|std::stringbuf}}|{{co|std::basic_stringbuf<char>}}}}
 +
{{dsc|{{lc|std::wstringbuf}}|{{co|std::basic_stringbuf<wchar_t>}}}}
 +
{{dsc|{{lc|std::istringstream}}|{{co|std::basic_istringstream<char>}}}}
 +
{{dsc|{{lc|std::wistringstream}}|{{co|std::basic_istringstream<wchar_t>}}}}
 +
{{dsc|{{lc|std::ostringstream}}|{{co|std::basic_ostringstream<char>}}}}
 +
{{dsc|{{lc|std::wostringstream}}|{{co|std::basic_ostringstream<wchar_t>}}}}
 +
{{dsc|{{lc|std::stringstream}}|{{co|std::basic_stringstream<char>}}}}
 +
{{dsc|{{lc|std::wstringstream}}|{{co|std::basic_stringstream<wchar_t>}}}}
 +
{{dsc header|spanstream}}
 +
{{dsc|{{lc|std::spanbuf}} {{mark c++23}}|{{co|std::basic_spanbuf<char>}}}}
 +
{{dsc|{{lc|std::wspanbuf}} {{mark c++23}}|{{co|std::basic_spanbuf<wchar_t>}}}}
 +
{{dsc|{{lc|std::ispanstream}} {{mark c++23}}|{{co|std::basic_ispanstream<char>}}}}
 +
{{dsc|{{lc|std::wispanstream}} {{mark c++23}}|{{co|std::basic_ispanstream<wchar_t>}}}}
 +
{{dsc|{{lc|std::ospanstream}} {{mark c++23}}|{{co|std::basic_ospanstream<char>}}}}
 +
{{dsc|{{lc|std::wospanstream}} {{mark c++23}}|{{co|std::basic_ospanstream<wchar_t>}}}}
 +
{{dsc|{{lc|std::spanstream}} {{mark c++23}}|{{co|std::basic_spanstream<char>}}}}
 +
{{dsc|{{lc|std::wspanstream}} {{mark c++23}}|{{co|std::basic_spanstream<wchar_t>}}}}
 +
{{dsc header|syncstream}}
 +
{{dsc|{{lc|std::syncbuf}} {{mark c++20}}|{{co|std::basic_syncbuf<char>}}}}
 +
{{dsc|{{lc|std::wsyncbuf}} {{mark c++20}}|{{co|std::basic_syncbuf<wchar_t>}}}}
 +
{{dsc|{{lc|std::osyncstream}} {{mark c++20}}|{{co|std::basic_osyncstream<char>}}}}
 +
{{dsc|{{lc|std::wosyncstream}} {{mark c++20}}|{{co|std::basic_osyncstream<wchar_t>}}}}
 +
{{dsc end}}
  
The following typedefs for common character types are provided:
+
====Predefined standard stream objects====
 +
{{dsc begin}}
 +
{{dsc header|iostream}}
 +
{{dsc inc|cpp/io/dsc cin}}
 +
{{dsc inc|cpp/io/dsc cout}}
 +
{{dsc inc|cpp/io/dsc cerr}}
 +
{{dsc inc|cpp/io/dsc clog}}
 +
{{dsc end}}
  
<source lang="cpp">
+
===={{rl|manip|I/O Manipulators}}====
typedef basic_ios<char>                ios;
+
The stream-based I/O library uses {{rl|manip|I/O manipulators}} (e.g. {{lc|std::boolalpha}}, {{lc|std::hex}}, etc.) to control how streams behave.
typedef basic_ios<wchar_t>            wios;
+
  
typedef basic_streambuf<char>    streambuf;
+
====Types====
typedef basic_streambuf<wchar_t> wstreambuf;
+
The following auxiliary types are defined:
typedef basic_filebuf<char>        filebuf;
+
{{dsc begin}}
typedef basic_filebuf<wchar_t>    wfilebuf;
+
{{dsc header|ios}}
typedef basic_stringbuf<char>    stringbuf;
+
{{dsc inc|cpp/io/dsc streamoff}}
typedef basic_stringbuf<wchar_t> wstringbuf;
+
{{dsc inc|cpp/io/dsc streamsize}}
 +
{{dsc inc|cpp/io/dsc fpos}}
 +
{{dsc end}}
  
typedef basic_istream<char>         istream;
+
The following typedef names for {{c/core|std::fpos<std::mbstate_t>}} are provided:
typedef basic_istream<wchar_t>    wistream;
+
{{dsc begin}}
typedef basic_ostream<char>         ostream;
+
{{dsc header|iosfwd}}
typedef basic_ostream<wchar_t>     wostream;
+
{{dsc hitem|Type|Definition}}
typedef basic_iostream<char>       iostream;
+
{{dsc|{{tt|std::streampos}}|{{c/core|std::fpos<std::char_traits<char>::state_type>}}}}
typedef basic_iostream<wchar_t>   wiostream;
+
{{dsc|{{tt|std::wstreampos}}|{{c/core|std::fpos<std::char_traits<wchar_t>::state_type>}}}}
 +
{{dsc|{{tt|std::u8streampos}} {{mark c++20}}|{{c/core|std::fpos<std::char_traits<char8_t>::state_type>}}}}
 +
{{dsc|{{tt|std::u16streampos}} {{mark c++11}}|{{c/core|std::fpos<std::char_traits<char16_t>::state_type>}}}}
 +
{{dsc|{{tt|std::u32streampos}} {{mark c++11}}|{{c/core|std::fpos<std::char_traits<char32_t>::state_type>}}}}
 +
{{dsc end}}
  
typedef basic_ifstream<char>      ifstream;
+
====Error category interface====
typedef basic_ifstream<wchar_t>  wifstream;
+
{{dsc begin}}
typedef basic_ofstream<char>      ofstream;
+
{{dsc header|ios}}
typedef basic_ofstream<wchar_t>  wofstream;
+
{{dsc inc|cpp/io/dsc io_errc}}
typedef basic_fstream<char>        fstream;
+
{{dsc inc|cpp/io/dsc iostream_category}}
typedef basic_fstream<wchar_t>    wfstream;
+
{{dsc end}}
  
typedef basic_istringstream<char>    istringstream;
+
{{anchor|Print functions}}
typedef basic_istringstream<wchar_t> wistringstream;
+
===Print functions {{mark since c++23}}===
typedef basic_ostringstream<char>    ostringstream;
+
The Unicode-aware print-family functions that perform formatted I/O on text that is already formatted. They bring all the performance benefits of {{lc|std::format}}, are locale-independent by default, reduce global state, avoid allocating a temporary {{lc|std::string}} object and calling {{c|operator<<}}, and in general make formatting more efficient compared to [[#Stream-based I/O|iostreams]] and [[#C-style I/O|stdio]].
typedef basic_ostringstream<wchar_t> wostringstream;
+
typedef basic_stringstream<char>      stringstream;
+
typedef basic_stringstream<wchar_t>  wstringstream;
+
</source>
+
  
Predefined standard stream objects:
+
The following print-like functions are provided:
<source lang="cpp">
+
{{dsc begin}}
extern  istream  cin;  //standard input (stdin)
+
{{dsc header|print}}
extern wistream wcin;
+
{{dsc inc|cpp/io/dsc print}}
extern  ostream  cout;  //standard output (stdout)
+
{{dsc inc|cpp/io/dsc println}}
extern wostream wcout;
+
{{dsc inc|cpp/io/dsc vprint_unicode}}
extern  ostream  cerr;  //standard error (stderr)
+
{{dsc inc|cpp/io/dsc vprint_nonunicode}}
extern wostream wcerr;
+
{{dsc sep}}
extern  ostream  clog;  //standard log (stdlog)
+
{{dsc header|ostream}}
extern wostream wclog;
+
{{dsc inc|cpp/io/basic_ostream/dsc print}}
</source>
+
{{dsc inc|cpp/io/basic_ostream/dsc println}}
 
+
{{dsc end}}
====[[cpp/io/manip|I/O Manipulators]]====
+
 
+
The stream-based I/O library uses [[cpp/io/manip| I/O manipulators]] (e.g. {{c|std::boolalpha}}, {{c|std::hex}}, etc.) to control how streams behave.
+
 
+
====Types====
+
 
+
The following auxiliary type are defined:
+
{{dcl list begin}}
+
{{dcl list header | ios }}
+
{{dcl list typedef | cpp/io/streamoff | represents relative file/stream position (offset from fpos), sufficient to represent any file size }}
+
{{dcl list typedef | cpp/io/streamsize | represents the number of characters transferred in an I/O operation or the size of an I/O buffer}}
+
{{dcl list tclass  | cpp/io/fpos | represents absolute position in a stream or a file}}
+
{{dcl list end}}
+
 
+
Four specializations of {{c|std::fpos}} are provided:
+
{{tdcl list begin}}
+
{{tdcl list header | ios}}
+
{{tdcl list hitem | Type | Definition}}
+
{{tdcl list item | {{tt|streampos}} | {{c|std::fpos<std::char_traits<char>::state_type>}}}}
+
{{tdcl list item | {{tt|u16streampos}} | {{c|std::fpos<std::char_traits<char16_t>::state_type>}}}}
+
{{tdcl list item | {{tt|u32streampos}} | {{c|std::fpos<std::char_traits<char32_t>::state_type>}}}}
+
{{tdcl list item | {{tt|wstreampos}} | {{c|std::fpos<std::char_traits<wchar_t>::state_type>}}}}
+
{{tdcl list end}}
+
 
+
====Error category interface====
+
{{dcl list begin}}
+
{{dcl list header | ios }}
+
{{dcl list template | cpp/io/dcl list io_errc}}
+
{{dcl list template | cpp/io/dcl list iostream_category}}
+
<!-- TODO: these three specializations go inside the io_errc page
+
{{dcl list ptfun | cpp/io/is_error_code_enum  | title=is_error_code_enum{{dcl small|(std::io_errc)}}| notes={{mark c++11}} | extends the type trait {{c|std::is_error_code_enum}} to identify iostream error codes}}
+
{{dcl list fun | cpp/io/make_error_code | title=make_error_code{{dcl small|(std::io_errc)}} | notes={{mark c++11}} | constructs an iostream error code}}
+
{{dcl list fun | cpp/io/make_error_condition  | title=make_error_condition{{dcl small|(std::io_errc)}} | notes={{mark c++11}} | constructs an iostream error_condition}}
+
-->
+
{{dcl list end}}
+
  
===[[cpp/io/c | C-style IO]]===
+
==={{rl|c|C-style I/O}}===
 +
C++ also includes the {{rl|c|input/output functions defined by C}}, such as {{lc|std::fopen}}, {{lc|std::getc}}, etc.
  
C++ also includes the [[cpp/io/c | input/output functions defined by C]], such as {{c|std::fopen}}, {{c|std::getc}}, etc.
+
===See also===
 +
{{dsc begin}}
 +
{{dsc|[[cpp/filesystem|Filesystem library]] {{mark since c++17}}}}
 +
{{dsc end}}
  
[[it:cpp/io]]
+
{{langlinks|ar|de|es|fr|it|ja|pl|pt|ru|zh}}
[[ja:cpp/io]]
+
[[pl:cpp/io]]
+
[[ru:cpp/io]]
+
[[zh:cpp/io]]
+

Latest revision as of 07:56, 9 June 2024

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:

cpp/io/ios basecpp/io/basic ioscpp/io/basic istreamcpp/io/basic ifstreamcpp/io/basic istringstreamcpp/io/basic ostreamcpp/io/basic ofstreamcpp/io/basic ostringstreamcpp/io/basic fstreamcpp/io/basic stringstreamcpp/io/basic iostreamstd-io-complete-inheritance.svg

Inheritance diagram

Abstraction
Defined in header <ios>
manages formatting flags and input/output exceptions
(class) [edit]
manages an arbitrary stream buffer
(class template) [edit]
Defined in header <streambuf>
abstracts a raw device
(class template) [edit]
Defined in header <ostream>
wraps a given abstract device (std::basic_streambuf)
and provides high-level output interface
(class template) [edit]
Defined in header <istream>
wraps a given abstract device (std::basic_streambuf)
and provides high-level input interface
(class template) [edit]
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 <fstream>
implements raw file device
(class template) [edit]
implements high-level file stream input operations
(class template) [edit]
implements high-level file stream output operations
(class template) [edit]
implements high-level file stream input/output operations
(class template) [edit]
String I/O implementation
Defined in header <sstream>
implements raw string device
(class template) [edit]
implements high-level string stream input operations
(class template) [edit]
implements high-level string stream output operations
(class template) [edit]
implements high-level string stream input/output operations
(class template) [edit]
Array I/O implementations
Defined in header <spanstream>
implements raw fixed character buffer device
(class template) [edit]
implements fixed character buffer input operations
(class template) [edit]
implements fixed character buffer output operations
(class template) [edit]
implements fixed character buffer input/output operations
(class template) [edit]
Defined in header <strstream>
(deprecated in C++98)(removed in C++26)
implements raw character array device
(class) [edit]
(deprecated in C++98)(removed in C++26)
implements character array input operations
(class) [edit]
(deprecated in C++98)(removed in C++26)
implements character array output operations
(class) [edit]
(deprecated in C++98)(removed in C++26)
implements character array input/output operations
(class) [edit]
Synchronized output
Defined in header <syncstream>
synchronized output device wrapper
(class template) [edit]
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 <ios>
std::ios std::basic_ios<char>
std::wios std::basic_ios<wchar_t>
Defined in header <streambuf>
std::streambuf std::basic_streambuf<char>
std::wstreambuf std::basic_streambuf<wchar_t>
Defined in header <istream>
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 <ostream>
std::ostream std::basic_ostream<char>
std::wostream std::basic_ostream<wchar_t>
Defined in header <fstream>
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 <sstream>
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 <spanstream>
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 <syncstream>
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

Defined in header <iostream>
reads from the standard C input stream stdin
(global object)[edit]
writes to the standard C output stream stdout
(global object)[edit]
writes to the standard C error stream stderr, unbuffered
(global object)[edit]
writes to the standard C error stream stderr
(global object)[edit]

[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 <ios>
represents relative file/stream position (offset from fpos), sufficient to represent any file size
(typedef) [edit]
represents the number of characters transferred in an I/O operation or the size of an I/O buffer
(typedef) [edit]
represents absolute position in a stream or a file
(class template) [edit]

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

Defined in header <iosfwd>
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

Defined in header <ios>
(C++11)
the IO stream error codes
(enum) [edit]
identifies the iostream error category
(function) [edit]

[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]
(C++23)
same as std::print except that each print is terminated by additional new line
(function template) [edit]
prints to Unicode capable stdout or a file stream using type-erased argument representation
(function) [edit]
prints to stdout or a file stream using type-erased argument representation
(function) [edit]
 
Defined in header <ostream>
outputs formatted representation of the arguments
(function template) [edit]
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

Filesystem library (since C++17)