Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io"

From cppreference.com
< cpp
m (+ wiki link to OOP)
(Stream-based I/O: P0448R4 spanstream)
Line 5: Line 5:
  
 
===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.
  
Line 37: Line 36:
 
{{dsc inc | cpp/io/dsc basic_stringstream}}
 
{{dsc inc | cpp/io/dsc basic_stringstream}}
 
{{dsc h2 | Array I/O implementations}}
 
{{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 header | strstream }}
 
{{dsc inc | cpp/io/dsc strstreambuf}}
 
{{dsc inc | cpp/io/dsc strstreambuf}}
Line 49: Line 53:
  
 
====Typedefs====
 
====Typedefs====
 
 
The following typedefs for common character types are provided:
 
The following typedefs for common character types are provided:
  
Line 62: Line 65:
 
typedef basic_stringbuf<char>    stringbuf;
 
typedef basic_stringbuf<char>    stringbuf;
 
typedef basic_stringbuf<wchar_t> wstringbuf;
 
typedef basic_stringbuf<wchar_t> wstringbuf;
typedef basic_syncbuf<char>        syncbuf;
+
typedef basic_syncbuf<char>        syncbuf; // C++20
typedef basic_syncbuf<wchar_t>    wsyncbuf;
+
typedef basic_syncbuf<wchar_t>    wsyncbuf; // C++20
 +
typedef basic_spanbuf<char>        spanbuf; // C++23
 +
typedef basic_spanbuf<wchar_t>    wspanbuf; // C++23
  
 
typedef basic_istream<char>        istream;
 
typedef basic_istream<char>        istream;
Line 86: Line 91:
 
typedef basic_stringstream<wchar_t>  wstringstream;
 
typedef basic_stringstream<wchar_t>  wstringstream;
  
typedef basic_osyncstream<char>    osyncstream;
+
typedef basic_osyncstream<char>    osyncstream; // C++20
typedef basic_osyncstream<wchar_t> wosyncstream;
+
typedef basic_osyncstream<wchar_t> wosyncstream; // C++20
 +
 
 +
typedef basic_ispanstream<char>    ispanstream; // C++23
 +
typedef basic_ispanstream<wchar_t> wispanstream; // C++23
 +
typedef basic_ospanstream<char>    ospanstream; // C++23
 +
typedef basic_ospanstream<wchar_t> wospanstream; // C++23
 +
typedef basic_spanstream<char>      spanstream; // C++23
 +
typedef basic_spanstream<wchar_t>  wspanstream; // C++23
 
}}
 
}}
  
Line 104: Line 116:
  
 
====Types====
 
====Types====
 
 
The following auxiliary types are defined:
 
The following auxiliary types are defined:
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | ios }}
+
{{dsc header | ios}}
 
{{dsc inc | cpp/io/dsc streamoff}}
 
{{dsc inc | cpp/io/dsc streamoff}}
 
{{dsc inc | cpp/io/dsc streamsize}}
 
{{dsc inc | cpp/io/dsc streamsize}}
Line 113: Line 124:
 
{{dsc end}}
 
{{dsc end}}
  
The following specializations of {{lc|std::fpos}} are provided:  
+
The following typedef names for {{c|std::fpos<std::mbstate_t>}} are provided:  
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | ios}}
+
{{dsc header | iosfwd}}
 
{{dsc hitem | Type | Definition}}
 
{{dsc hitem | Type | Definition}}
 
{{dsc | {{tt|streampos}} | {{c|std::fpos<std::char_traits<char>::state_type>}}}}
 
{{dsc | {{tt|streampos}} | {{c|std::fpos<std::char_traits<char>::state_type>}}}}
 
{{dsc | {{tt|wstreampos}} | {{c|std::fpos<std::char_traits<wchar_t>::state_type>}}}}
 
{{dsc | {{tt|wstreampos}} | {{c|std::fpos<std::char_traits<wchar_t>::state_type>}}}}
 +
{{dsc | {{tt|u8streampos}} {{mark c++20}} | {{c|std::fpos<std::char_traits<char8_t>::state_type>}}}}
 +
{{dsc | {{tt|u16streampos}} {{mark c++11}} | {{c|std::fpos<std::char_traits<char16_t>::state_type>}}}}
 +
{{dsc | {{tt|u32streampos}} {{mark c++11}} | {{c|std::fpos<std::char_traits<char32_t>::state_type>}}}}
 
{{dsc end}}
 
{{dsc end}}
  
 
====Error category interface====
 
====Error category interface====
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | ios }}
+
{{dsc header | ios}}
 
{{dsc inc | cpp/io/dsc io_errc}}
 
{{dsc inc | cpp/io/dsc io_errc}}
 
{{dsc inc | cpp/io/dsc iostream_category}}
 
{{dsc inc | cpp/io/dsc iostream_category}}

Revision as of 22:42, 10 June 2021

C++ includes two input/output libraries: an OOP-style stream-based I/O library and the standard set of C-style I/O functions.

Contents

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]

Typedefs

The following typedefs for common character types are provided:

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

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]

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.

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
streampos std::fpos<std::char_traits<char>::state_type>
wstreampos std::fpos<std::char_traits<wchar_t>::state_type>
u8streampos (C++20) std::fpos<std::char_traits<char8_t>::state_type>
u16streampos (C++11) std::fpos<std::char_traits<char16_t>::state_type>
u32streampos (C++11) std::fpos<std::char_traits<char32_t>::state_type>

Error category interface

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

C-style I/O

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