Input/output library
C++は以下の入出力ライブラリを含みます: OOPスタイル の ストリームベースI/O ライブラリ 、 プリントベース 関数ファミリー (C++23以降) 、および標準的な CスタイルI/O 関数群。
目次 |
ストリームベースI/O
ストリームベースの入出力ライブラリは、抽象的な入出力デバイスを中心に構成されています。これらの抽象デバイスにより、ファイルへの入出力、メモリストリーム、あるいは任意の操作(例:圧縮)をオンザフライで実行するカスタムアダプタデバイスに対して、同じコードで処理することが可能になります。
ほとんどのクラスはテンプレート化されているため、あらゆる基本文字型に適合させることができます。最も一般的な基本文字型( char および wchar_t )に対しては、個別のtypedefが提供されています。クラスは以下の階層構造で整理されています:
継承図
抽象化 |
|
|
ヘッダーで定義
<ios>
|
|
|
書式フラグと入出力例外を管理する
(クラス) |
|
|
任意のストリームバッファを管理する
(クラステンプレート) |
|
|
ヘッダーで定義
<streambuf>
|
|
|
生デバイスを抽象化する
(クラステンプレート) |
|
|
ヘッダーで定義
<ostream>
|
|
|
与えられた抽象デバイス(
std::basic_streambuf
)をラップし、
高レベルな出力インターフェースを提供する (クラステンプレート) |
|
|
ヘッダーで定義
<istream>
|
|
|
指定された抽象デバイス(
std::basic_streambuf
)をラップし、
高レベルな入力インターフェースを提供する (クラステンプレート) |
|
|
指定された抽象デバイス(
std::basic_streambuf
)をラップし、
高レベルな入出力インターフェースを提供する (クラステンプレート) |
|
ファイルI/O実装 |
|
|
ヘッダーで定義
<fstream>
|
|
|
生ファイルデバイスを実装する
(クラステンプレート) |
|
|
高レベルファイルストリーム入力操作を実装する
(クラステンプレート) |
|
|
高レベルファイルストリーム出力操作を実装する
(クラステンプレート) |
|
|
高レベルファイルストリーム入出力操作を実装する
(クラステンプレート) |
|
String I/O 実装 |
|
|
ヘッダーで定義
<sstream>
|
|
|
生文字列デバイスを実装する
(クラステンプレート) |
|
|
高レベル文字列ストリーム入力操作を実装する
(クラステンプレート) |
|
|
高レベル文字列出力操作を実装する
(クラステンプレート) |
|
|
高レベル文字列ストリーム入出力操作を実装する
(クラステンプレート) |
|
配列I/O実装 |
|
|
ヘッダーで定義
<spanstream>
|
|
|
(C++23)
|
生の固定文字バッファデバイスを実装する
(クラステンプレート) |
|
(C++23)
|
固定文字バッファ入力操作を実装する
(クラステンプレート) |
|
(C++23)
|
固定文字バッファの出力操作を実装する
(クラステンプレート) |
|
(C++23)
|
固定文字バッファの入出力操作を実装する
(クラステンプレート) |
|
ヘッダーで定義
<strstream>
|
|
|
(C++98で非推奨)
(C++26で削除)
|
生文字配列デバイスを実装する
(クラス) |
|
(C++98で非推奨)
(C++26で削除)
|
文字配列入力操作を実装する
(クラス) |
|
(C++98で非推奨)
(C++26で削除)
|
文字配列出力操作を実装する
(クラス) |
|
(C++98で非推奨)
(C++26で削除)
|
文字配列入出力操作を実装する
(クラス) |
同期出力 (C++20以降) |
|
|
ヘッダーで定義
<syncstream>
|
|
|
(C++20)
|
同期出力デバイスラッパー
(クラステンプレート) |
|
(C++20)
|
同期化出力ストリームラッパー
(クラステンプレート) |
Typedefs
以下の一般的な文字型のための typedef が名前空間
std
で提供されています:
| 型 | 定義 |
|
ヘッダーで定義
<ios>
|
|
| std::ios | std:: basic_ios < char > |
| std::wios | std:: basic_ios < wchar_t > |
|
ヘッダーで定義
<streambuf>
|
|
| std::streambuf | std:: basic_streambuf < char > |
| std::wstreambuf | std:: basic_streambuf < wchar_t > |
|
ヘッダーで定義
<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 > |
|
ヘッダーで定義
<ostream>
|
|
| std::ostream | std:: basic_ostream < char > |
| std::wostream | std:: basic_ostream < wchar_t > |
|
ヘッダーで定義
<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 > |
|
ヘッダーで定義
<sstream>
|
|
| std::stringbuf | std:: basic_stringbuf < char > |
| std::wstringbuf | std:: basic_stringbuf < wchar_t > |
| 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::wostringstream | std:: basic_ostringstream < wchar_t > |
| std::stringstream | std:: basic_stringstream < char > |
| std::wstringstream | std:: basic_stringstream < wchar_t > |
| std::wstringstream | std:: basic_stringstream < wchar_t > |
|
ヘッダーで定義
<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 > |
|
ヘッダーで定義
<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 > |
事前定義済み標準ストリームオブジェクト
|
ヘッダーで定義
<iostream>
|
|
|
標準C入力ストリーム
stdin
から読み取る
(グローバルオブジェクト) |
|
|
標準C出力ストリーム
stdout
に書き込む
(グローバルオブジェクト) |
|
|
標準Cエラーストリーム
stderr
に書き込む(バッファリングなし)
(グローバルオブジェクト) |
|
|
標準Cエラーストリーム
stderr
に書き込む
(グローバルオブジェクト) |
|
I/O マニピュレータ
ストリームベースのI/Oライブラリは、 I/Oマニピュレータ (例: std::boolalpha 、 std::hex など)を使用して、ストリームの動作を制御します。
型
以下の補助型が定義されています:
|
ヘッダーで定義
<ios>
|
|
|
相対的なファイル/ストリーム位置(fposからのオフセット)を表し、任意のファイルサイズを表現するのに十分
(typedef) |
|
|
I/O操作で転送される文字数またはI/Oバッファのサイズを表す
(typedef) |
|
|
ストリームまたはファイル内の絶対位置を表す
(クラステンプレート) |
|
以下の typedef 名が std:: fpos < std:: mbstate_t > に対して提供されています:
|
ヘッダで定義
<iosfwd>
|
|
| 型 | 定義 |
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 > |
エラーカテゴリインターフェース (C++11以降)
|
ヘッダーで定義
<ios>
|
|
|
(C++11)
|
IOストリームエラーコード
(列挙型) |
|
(C++11)
|
iostreamエラーカテゴリを識別する
(関数) |
出力関数 (C++23以降)
既にフォーマット済みのテキストに対して書式付きI/Oを実行するUnicode対応のprint系関数。これらは std::format のすべてのパフォーマンス上の利点を提供し、デフォルトでロケール非依存、グローバル状態の削減、一時的な std::string オブジェクトの割り当てと operator << の呼び出しを回避し、一般的に iostreams や stdio と比較してフォーマットをより効率的に行います。
以下の出力形式の関数が提供されています:
|
ヘッダーで定義
<print>
|
|
|
(C++23)
|
引数の
フォーマット済み
表現を使用して
stdout
またはファイルストリームに出力
(関数テンプレート) |
|
(C++23)
|
std::print
と同じだが、各出力の末尾に改行が追加される
(関数テンプレート) |
|
Unicode対応の
stdout
またはファイルストリームに
型消去された
引数表現を使用して出力
(関数) |
|
|
stdout
またはファイルストリームに
型消去された
引数表現を使用して出力
(関数) |
|
|
ヘッダーで定義
<ostream>
|
|
|
(C++23)
|
引数の
フォーマット済み
表現を出力
(関数テンプレート) |
|
(C++23)
|
引数の
フォーマット済み
表現を出力し、末尾に
'
\n
'
を追加
(関数テンプレート) |
CスタイルI/O
C++はまた、 Cで定義された入出力関数 も含みます。例えば std::fopen 、 std::getc などです。
関連項目
| ファイルシステムライブラリ (C++17以降) |