Namespaces
Variants

std:: basic_fstream

From cppreference.net
< cpp ‎ | io
ヘッダーで定義 <fstream>
template <

class CharT,
class Traits = std:: char_traits < CharT >

> class basic_fstream : public std:: basic_iostream < CharT, Traits >

クラステンプレート basic_fstream は、ファイルベースのストリームに対する高レベルな入出力操作を実装します。これはファイルベースのストリームバッファ( std::basic_filebuf )を高レベルインターフェース( std::basic_iostream )と連携させます。

std::basic_fstream の典型的な実装は、非継承データメンバを1つだけ保持します: std:: basic_filebuf < CharT, Traits > のインスタンスです。

cpp/io/ios base cpp/io/basic ios cpp/io/basic istream cpp/io/basic ostream cpp/io/basic iostream std-basic fstream-inheritance.svg

継承図

一般的な文字型に対するいくつかの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)
基盤となる実装定義のハンドルを返す
(public member function)
ファイル操作
ストリームに関連付けられたファイルがあるかどうかをチェックする
(public member function)
ファイルを開き、ストリームに関連付ける
(public member function)
関連付けられたファイルを閉じる
(public member function)

非メンバー関数

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メンバー関数)
C++とCのI/Oライブラリが相互運用可能かどうかを設定する
( std::ios_base のpublic staticメンバー関数)
メンバークラス
ストリーム例外
( std::ios_base のpublicメンバークラス)
標準ストリームオブジェクトを初期化する
( std::ios_base のpublicメンバークラス)

メンバー型と定数

説明
ストリームオープンモード型

以下の定数も定義されています:

定数 説明
app 各書き込み前にストリームの終端へシーク
binary バイナリモード でオープン
in 読み取り用にオープン
out 書き込み用にオープン
trunc オープン時にストリームの内容を破棄
ate オープン直後にストリームの終端へシーク
noreplace (C++23) 排他モードでオープン

(typedef)
書式フラグ型

以下の定数も定義されています:

定数 説明
dec 整数I/Oに10進基数を使用:参照 std::dec
oct 整数I/Oに8進基数を使用:参照 std::oct
hex 整数I/Oに16進基数を使用:参照 std::hex
basefield dec | oct | hex 。マスキング操作に有用
left 左揃え(右側に埋め文字を追加):参照 std::left
right 右揃え(左側に埋め文字を追加):参照 std::right
internal 内部揃え(内部指定点に埋め文字を追加):参照 std::internal
adjustfield left | right | internal 。マスキング操作に有用
scientific 浮動小数点型を指数表記で生成、または fixed と組み合わせた場合は16進表記:参照 std::scientific
fixed 浮動小数点型を固定小数点表記で生成、または scientific と組み合わせた場合は16進表記:参照 std::fixed
floatfield scientific | fixed 。マスキング操作に有用
boolalpha bool 型を英数字形式で挿入・抽出:参照 std::boolalpha
showbase 整数出力で数値基数を示す接頭辞を生成、通貨I/Oで通貨指示子を要求:参照 std::showbase
showpoint 浮動小数点数出力で無条件に小数点文字を生成:参照 std::showpoint
showpos 非負数値出力で + 文字を生成:参照 std::showpos
skipws 特定の入力操作前に先頭の空白をスキップ:参照 std::skipws
unitbuf 各出力操作後に出力をフラッシュ:参照 std::unitbuf
uppercase 特定の出力操作で特定の小文字を対応する大文字に置換:参照 std::uppercase

(typedef)
ストリームの状態を表す型

以下の定数も定義されています:

定数 説明
goodbit エラーなし
badbit 回復不能なストリームエラー
failbit 入出力操作の失敗(書式化または抽出エラー)
eofbit 関連付けられた入力シーケンスがファイルの終端に到達

(typedef)
シーク方向の型

以下の定数も定義されています:

定数 説明
beg ストリームの先頭
end ストリームの末尾
cur ストリーム位置指示子の現在位置

(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ストリームからデータを文字列に読み込む
(関数テンプレート)
翻訳のポイント: - `getline` はC++の関数名なので翻訳せず保持 - `read data from an I/O stream into a string` → `I/Oストリームからデータを文字列に読み込む` - `function template` → `関数テンプレート` - HTMLタグ、属性、構造は完全に保持 - 技術文書としての正確性と専門性を維持