std::basic_fstream
From cppreference.com
| ヘッダ <fstream> で定義 |
||
| template< class CharT, |
||
クラス テンプレート basic_fstream は、ファイルベースのストリームに対する高レベルな入出力操作を実装します。ファイルベースのストリームバッファ (std::basic_filebuf) と高レベルなインターフェース (std::basic_iostream) を接続します。
std::basic_fstream の典型的な実装は、非派生データメンバを1つだけ保持します。それは、std::basic_filebuf<CharT, Traits> のインスタンスです。
継承図
一般的な文字型のためにいくつかの typedef が提供されています
| ヘッダ
<fstream> で定義 | |
| 型 | 定義 |
std::fstream
|
std::basic_fstream<char> |
std::wfstream
|
std::basic_fstream<wchar_t> |
目次 |
[編集] メンバ型
| メンバ型 | 定義 |
char_type
|
CharT |
traits_type
|
Traits; Traits::char_type が CharT でない場合、プログラムは不適格となる。 |
int_type
|
Traits::int_type |
pos_type
|
Traits::pos_type |
off_type
|
Traits::off_type |
native_handle_type(C++26) |
TriviallyCopyable および semiregular である実装定義型 |
[編集] メンバ関数
| ファイルストリームを構築する (public member function) | |
| (デストラクタ) [仮想] (暗黙的に宣言される) |
basic_fstream と関連バッファを破棄し、ファイルを閉じる(virtual public member function) |
| (C++11) |
ファイルストリームを移動する (public member function) |
| (C++11) |
2つのファイルストリームをスワップする (public member function) |
| 基となる生のファイルデバイスオブジェクトを返す (public member function) | |
| (C++26) |
基となる実装定義のハンドルを返す (public メンバ関数) |
ファイル操作 | |
| ストリームに関連付けられたファイルがあるかチェックする (public member function) | |
| ファイルを開き、ストリームに関連付ける (public member function) | |
| 関連付けられたファイルを閉じる (public member function) | |
[編集] 非メンバ関数
| std::swap アルゴリズムを特殊化する (function template) |
std::basic_istream から継承
メンバ関数
書式付き入力 | |
| 書式付きデータを抽出する ( std::basic_istream<CharT,Traits>のpublicメンバ関数) | |
書式なし入力 | |
| 文字を抽出する ( std::basic_istream<CharT,Traits> の public メンバ関数) | |
| 次の文字を抽出せずに読み取る ( std::basic_istream<CharT,Traits> の public メンバ関数) | |
| 文字を非抽出する (un-extract) ( std::basic_istream<CharT,Traits> の public メンバ関数) | |
| 文字を入力ストリームに戻す ( std::basic_istream<CharT,Traits> の public メンバ関数) | |
| 指定された文字が見つかるまで文字を抽出する ( std::basic_istream<CharT,Traits> の public メンバ関数) | |
| 指定された文字が見つかるまで文字を抽出して破棄する ( std::basic_istream<CharT,Traits> の public メンバ関数) | |
| 文字のブロックを抽出する ( std::basic_istream<CharT,Traits> の public メンバ関数) | |
| すでに利用可能な文字のブロックを抽出する ( std::basic_istream<CharT,Traits> の public メンバ関数) | |
| 最後の書式なし入力操作によって抽出された文字数を返す ( std::basic_istream<CharT,Traits> の public メンバ関数) | |
位置付け | |
| 入力位置インジケータを返す ( std::basic_istream<CharT,Traits> の public メンバ関数) | |
| 入力位置インジケータを設定する ( std::basic_istream<CharT,Traits> の public メンバ関数) | |
その他 | |
| 基礎となるストレージデバイスと同期する ( std::basic_istream<CharT,Traits> の public メンバ関数) | |
メンバクラス
| 入力操作のためのストリームの準備に関する基本的なロジックを実装する ( std::basic_istream<CharT,Traits> の public メンバクラス) |
std::basic_ostream から継承
メンバ関数
書式付き出力 | |
| 書式付きデータを挿入する ( std::basic_ostream<CharT,Traits>のpublicメンバ関数) | |
非書式付き出力 | |
| 文字を挿入する ( std::basic_ostream<CharT,Traits> の public メンバ関数) | |
| 文字ブロックを挿入する ( std::basic_ostream<CharT,Traits> の public メンバ関数) | |
位置付け | |
| 出力位置インジケータを返す ( std::basic_ostream<CharT,Traits> の public メンバ関数) | |
| 出力位置インジケータを設定する ( std::basic_ostream<CharT,Traits> の public メンバ関数) | |
その他 | |
| 基礎となるストレージデバイスと同期する ( std::basic_ostream<CharT,Traits> の public メンバ関数) | |
メンバクラス
| 出力操作のためにストリームを準備する基本的なロジックを実装する ( std::basic_ostream<CharT,Traits> の public メンバクラス) |
std::basic_ios から継承
メンバ型
| メンバ型 | 定義 |
char_type
|
CharT
|
traits_type
|
Traits
|
int_type
|
Traits::int_type
|
pos_type
|
Traits::pos_type
|
off_type
|
Traits::off_type
|
メンバ関数
状態関数 | |
| エラーが発生していないか、すなわちI/O操作が可能かを確認する ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
| ファイルの終端に達したかを確認する ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
| エラーが発生したかを確認する ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
| 回復不能なエラーが発生したかを確認する ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
| エラーが発生したかを確認する (fail() のシノニム) ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
エラーが発生していないかを確認する (!fail() のシノニム)( std::basic_ios<CharT,Traits> の public メンバ関数) | |
| 状態フラグを返す ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
| 状態フラグを設定する ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
| 状態フラグを変更する ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
書式化 | |
| 書式化情報をコピーする ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
| フィル文字を管理する ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
その他 | |
| 例外マスクを管理する ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
| ロケールを設定する ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
| 関連付けられたストリームバッファを管理する ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
| 結びつけられたストリームを管理する ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
| 文字をナロー化する ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
| 文字をワイド化する ( std::basic_ios<CharT,Traits> の public メンバ関数) | |
std::ios_base から継承
メンバ関数
書式化 | |
| 書式フラグを管理する ( std::ios_base の public メンバ関数) | |
| 特定の書式フラグを設定する ( std::ios_base の public メンバ関数) | |
| 特定の書式フラグをクリアする ( std::ios_base の public メンバ関数) | |
| 浮動小数点数操作の小数点以下の精度を管理する ( std::ios_base の public メンバ関数) | |
| フィールド幅を管理する ( std::ios_base の public メンバ関数) | |
ロケール | |
| ロケールを設定する ( std::ios_base の public メンバ関数) | |
| 現在のロケールを返す ( std::ios_base の public メンバ関数) | |
内部拡張可能配列 | |
| [static] |
pword() と iword() のインデックスとして安全に使用できる、プログラム全体で一意な整数を返す ( std::ios_base の public static メンバ関数) |
| 必要に応じてプライベートストレージのサイズを変更し、指定されたインデックスの long 要素にアクセスする ( std::ios_base の public メンバ関数) | |
| 必要に応じてプライベートストレージのサイズを変更し、指定されたインデックスの void* 要素にアクセスする ( std::ios_base の public メンバ関数) | |
その他 | |
| イベントコールバック関数を登録する ( std::ios_base の public メンバ関数) | |
| [static] |
C++ と C の I/O ライブラリが相互運用可能かどうかを設定する ( std::ios_base の public static メンバ関数) |
メンバクラス | |
| ストリーム例外 ( std::ios_base の public メンバクラス) | |
| 標準ストリームオブジェクトを初期化する ( std::ios_base の public メンバクラス) | |
メンバ型と定数 | |||||||||||||||||||||||||||||||||||||||
| 型 | 説明 | ||||||||||||||||||||||||||||||||||||||
| ストリームオープンモードの型 以下の定数も定義される
(typedef) | |||||||||||||||||||||||||||||||||||||||
| 書式化フラグの型 以下の定数も定義される
(typedef) | |||||||||||||||||||||||||||||||||||||||
| ストリームの状態の型 以下の定数も定義される
(typedef) | |||||||||||||||||||||||||||||||||||||||
| シーク方向の型 以下の定数も定義される
(typedef) | |||||||||||||||||||||||||||||||||||||||
| イベントの種類を指定する (enum) | |||||||||||||||||||||||||||||||||||||||
| コールバック関数の型 (typedef) | |||||||||||||||||||||||||||||||||||||||
[編集] 備考
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_fstream_native_handle |
202306L |
(C++26) | ネイティブハンドルサポート |
[編集] 例
このコードを実行
#include <fstream> #include <iostream> #include <string> int main() { std::string filename{"test.bin"}; std::fstream s{filename, s.binary | s.trunc | s.in | s.out}; if (!s.is_open()) std::cout << "failed to open " << filename << '\n'; else { // write double d{3.14}; s.write(reinterpret_cast<char*>(&d), sizeof d); // binary output s << 123 << "abc"; // text output // for fstream, this moves the file position pointer (both put and get) s.seekp(0); // read d = 2.71828; s.read(reinterpret_cast<char*>(&d), sizeof d); // binary input int n; std::string str; if (s >> n >> str) // text input std::cout << "read back from file: " << d << ' ' << n << ' ' << str << '\n'; } }
出力
read back from file: 3.14 123 abc
[編集] 関連項目
| I/Oストリームから文字列にデータを読み込む (function template) |