std:: basic_fstream
|
ヘッダーで定義
<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)
|
implementation-defined
型であり、
TriviallyCopyable
かつ
semiregular
である。
|
メンバー関数
|
ファイルストリームを構築する
(public member function) |
|
|
(destructor)
[virtual]
(implicitly declared)
|
basic_fstream
と関連するバッファを破棄し、ファイルを閉じる
(virtual public member function) |
|
(C++11)
|
ファイルストリームを移動する
(public member function) |
|
(C++11)
|
2つのファイルストリームを交換する
(public member function) |
|
基盤となる生ファイルデバイスオブジェクトを返す
(public member function) |
|
|
(C++26)
|
基盤となる実装定義のハンドルを返す
(public member function) |
ファイル操作 |
|
|
ストリームに関連付けられたファイルがあるかどうかをチェックする
(public member function) |
|
|
ファイルを開き、ストリームに関連付ける
(public member function) |
|
|
関連付けられたファイルを閉じる
(public member function) |
|
非メンバー関数
|
(C++11)
|
std::swap
アルゴリズムを特殊化
(関数テンプレート) |
std::basic_istream から継承
メンバ関数
書式付き入力 |
|
|
書式付きデータを抽出
(
std::basic_istream<CharT,Traits>
の公開メンバ関数)
|
|
書式なし入力 |
|
|
文字を抽出
(
std::basic_istream<CharT,Traits>
の公開メンバ関数)
|
|
|
次の文字を抽出せずに読み取る
(
std::basic_istream<CharT,Traits>
の公開メンバ関数)
|
|
|
文字を非抽出状態に戻す
(
std::basic_istream<CharT,Traits>
の公開メンバ関数)
|
|
|
入力ストリームに文字を戻す
(
std::basic_istream<CharT,Traits>
の公開メンバ関数)
|
|
|
指定された文字が見つかるまで文字を抽出
(
std::basic_istream<CharT,Traits>
の公開メンバ関数)
|
|
|
指定された文字が見つかるまで文字を抽出して破棄
(
std::basic_istream<CharT,Traits>
の公開メンバ関数)
|
|
|
文字ブロックを抽出
(
std::basic_istream<CharT,Traits>
の公開メンバ関数)
|
|
|
利用可能な文字ブロックを抽出
(
std::basic_istream<CharT,Traits>
の公開メンバ関数)
|
|
|
最後の書式なし入力操作で抽出された文字数を返す
(
std::basic_istream<CharT,Traits>
の公開メンバ関数)
|
|
位置指定 |
|
|
入力位置指示子を返す
(
std::basic_istream<CharT,Traits>
の公開メンバ関数)
|
|
|
入力位置指示子を設定
(
std::basic_istream<CharT,Traits>
の公開メンバ関数)
|
|
その他 |
|
|
基盤となる記憶装置と同期
(
std::basic_istream<CharT,Traits>
の公開メンバ関数)
|
|
メンバクラス
|
入力操作のためのストリーム準備の基本ロジックを実装
(
std::basic_istream<CharT,Traits>
の公開メンバクラス)
|
std:: basic_ostream から継承
メンバ関数
書式付き出力 |
|
|
書式付きデータを挿入
(
std::basic_ostream<CharT,Traits>
の公開メンバ関数)
|
|
書式なし出力 |
|
|
文字を挿入
(
std::basic_ostream<CharT,Traits>
の公開メンバ関数)
|
|
|
文字ブロックを挿入
(
std::basic_ostream<CharT,Traits>
の公開メンバ関数)
|
|
位置指定 |
|
|
出力位置指示子を返す
(
std::basic_ostream<CharT,Traits>
の公開メンバ関数)
|
|
|
出力位置指示子を設定
(
std::basic_ostream<CharT,Traits>
の公開メンバ関数)
|
|
その他 |
|
|
基盤となる記憶装置と同期
(
std::basic_ostream<CharT,Traits>
の公開メンバ関数)
|
|
メンバクラス
|
出力操作のためのストリーム準備の基本ロジックを実装
(
std::basic_ostream<CharT,Traits>
の公開メンバクラス)
|
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>
の公開メンバ関数)
|
|
|
ファイル終端に達したかチェック
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
|
エラーが発生したかチェック
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
|
回復不可能なエラーが発生したかチェック
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
|
エラーが発生したかチェック (
fail()
の同義語)
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
エラーが発生していないかチェック (
!
fail()
の同義語)
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
|
状態フラグを返す
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
|
状態フラグを設定
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
|
状態フラグを変更
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
書式設定 |
|
|
書式情報をコピー
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
|
埋め文字を管理
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
その他 |
|
|
例外マスクを管理
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
|
ロケールを設定
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
|
関連付けられたストリームバッファを管理
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
|
関連付けられたストリームを管理
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
|
文字をナロー化
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
|
文字をワイド化
(
std::basic_ios<CharT,Traits>
の公開メンバ関数)
|
|
std:: ios_base から継承
メンバー関数
フォーマット |
|
|
フォーマットフラグを管理する
(
std::ios_base
のpublicメンバー関数)
|
|
|
特定のフォーマットフラグを設定する
(
std::ios_base
のpublicメンバー関数)
|
|
|
特定のフォーマットフラグをクリアする
(
std::ios_base
のpublicメンバー関数)
|
|
|
浮動小数点演算の10進精度を管理する
(
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) |
|||||||||||||||||||||||||||||||||||||||
|
イベント型を指定
(列挙型) |
|||||||||||||||||||||||||||||||||||||||
|
コールバック関数型
(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 { // 書き込み double d{3.14}; s.write(reinterpret_cast<char*>(&d), sizeof d); // バイナリ出力 s << 123 << "abc"; // テキスト出力 // fstreamの場合、これはファイル位置ポインタを移動します(putとgetの両方) s.seekp(0); // 読み込み d = 2.71828; s.read(reinterpret_cast<char*>(&d), sizeof d); // バイナリ入力 int n; std::string str; if (s >> n >> str) // テキスト入力 std::cout << "read back from file: " << d << ' ' << n << ' ' << str << '\n'; } }
出力:
read back from file: 3.14 123 abc
関連項目
|
I/Oストリームからデータを文字列に読み込む
(関数テンプレート) |