名前空間
変種
操作

std::basic_fstream

提供: cppreference.com
< cpp‎ | io
 
 
入出力ライブラリ
入出力マニピュレータ
Cスタイルの入出力
バッファ
(C++98で非推奨)
ストリーム
抽象
ファイル入出力
basic_fstream
文字列入出力
配列入出力
(C++98で非推奨)
(C++98で非推奨)
(C++98で非推奨)
同期化出力
エラーカテゴリインタフェース
(C++11)
 
 
ヘッダ <fstream> で定義
template<

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

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

クラステンプレート basic_fstream はファイルベースのストリームに対する高水準の入出力操作を実装します。 std::basic_iostream の高水準インタフェースを使用してファイルベースのストリームバッファ (std::basic_filebuf) とやりとりします。

std::basic_fstream の一般的な実装はひとつの非継承データメンバ、 std::basic_filebuf<CharT, Traits> のインスタンスのみを保持します。

cpp/io/ios basecpp/io/basic ioscpp/io/basic istreamcpp/io/basic ostreamcpp/io/basic iostreamstd-basic fstream-inheritance.svg
画像の詳細

継承図

一般的な文字型に対する2つの特殊化が定義されます。

ヘッダ <fstream> で定義
定義
fstream basic_fstream<char>
wfstream basic_fstream<wchar_t>

目次

[編集] メンバ型

メンバ型 定義
char_type CharT[edit]
traits_type TraitsTraits::char_typeCharT でない場合、プログラムは ill-formed です [edit]
int_type Traits::int_type[edit]
pos_type Traits::pos_type[edit]
off_type Traits::off_type[edit]

[編集] メンバ関数

ファイルストリームを構築します
(パブリックメンバ関数) [edit]
デストラクタ
[仮想] (暗黙に宣言)
basic_fstream および紐付けられているバッファを破棄し、ファイルを閉じます
(仮想パブリックメンバ関数) [edit]
(C++11)
ファイルストリームをムーブします
(パブリックメンバ関数) [edit]
(C++11)
2つのファイルストリームを入れ替えます
(パブリックメンバ関数) [edit]
ベースとなる生のファイルデバイスオブジェクトを返します
(パブリックメンバ関数) [edit]
ファイル操作
ストリームが紐付けられているファイルを持っているかどうか調べます
(パブリックメンバ関数) [edit]
ファイルを開き、それをストリームと紐付けます
(パブリックメンバ関数) [edit]
紐付けられているファイルを閉じます
(パブリックメンバ関数) [edit]

[編集] 非メンバ関数

std::swap アルゴリズムの特殊化
(関数テンプレート) [edit]

std::basic_istream から継承

メンバ関数

書式付き入力
書式付きデータを抽出します
(std::basic_istream<CharT,Traits>のパブリックメンバ関数) [edit]
書式なし入力
文字を抽出します
(std::basic_istream<CharT,Traits>のパブリックメンバ関数) [edit]
次の文字を抽出せずに読み取ります
(std::basic_istream<CharT,Traits>のパブリックメンバ関数) [edit]
文字の抽出を取り消します
(std::basic_istream<CharT,Traits>のパブリックメンバ関数) [edit]
入力ストリームに文字を戻します
(std::basic_istream<CharT,Traits>のパブリックメンバ関数) [edit]
指定された文字が見つかるまで文字を抽出します
(std::basic_istream<CharT,Traits>のパブリックメンバ関数) [edit]
指定された文字が見つかるまで文字を抽出して破棄します
(std::basic_istream<CharT,Traits>のパブリックメンバ関数) [edit]
文字のブロックを抽出します
(std::basic_istream<CharT,Traits>のパブリックメンバ関数) [edit]
すでに利用可能な文字のブロックを抽出します
(std::basic_istream<CharT,Traits>のパブリックメンバ関数) [edit]
最後の書式なし入力操作によって抽出された文字数を返します
(std::basic_istream<CharT,Traits>のパブリックメンバ関数) [edit]
位置設定
入力位置指示子を返します
(std::basic_istream<CharT,Traits>のパブリックメンバ関数) [edit]
入力位置指示子を設定します
(std::basic_istream<CharT,Traits>のパブリックメンバ関数) [edit]
その他
ベースとなるストレージデバイスと同期します
(std::basic_istream<CharT,Traits>のパブリックメンバ関数) [edit]

メンバクラス

入力操作用にストリームを準備するための基本ロジックを実装します
(std::basic_istream<CharT,Traits>のパブリックメンバクラス) [edit]

std::basic_ostream から継承

メンバ関数

書式付き出力
書式付きデータを挿入します
(std::basic_ostream<CharT,Traits>のパブリックメンバ関数) [edit]
書式なし出力
文字を挿入します
(std::basic_ostream<CharT,Traits>のパブリックメンバ関数) [edit]
文字のブロックを挿入します
(std::basic_ostream<CharT,Traits>のパブリックメンバ関数) [edit]
位置設定
出力位置指示子を返します
(std::basic_ostream<CharT,Traits>のパブリックメンバ関数) [edit]
出力位置指示子を設定します
(std::basic_ostream<CharT,Traits>のパブリックメンバ関数) [edit]
その他
ベースとなるストレージデバイスと同期します
(std::basic_ostream<CharT,Traits>のパブリックメンバ関数) [edit]

メンバクラ���

出力操作用にストリームを準備するための基本ロジックを実装します
(std::basic_ostream<CharT,Traits>のパブリックメンバクラス) [edit]

std::basic_ios から継承

メンバ型

メンバ型 定義
char_type CharT
traits_type Traits
int_type Traits::int_type
pos_type Traits::pos_type
off_type Traits::off_type

メンバ関数

状態関数
エラーが発生していない (すなわち入出力操作が使用可能) かどうか調べます
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
ファイル終端に達したかどうか調べます
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
エラーが発生したかどうか調べます
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
回復不可能なエラーが発生したかどうか調べます
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
エラーが発生したかどうか調べます (fail() の同義語です)
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
(C++11未満)(C++11以上)
エラーが発生していないかどうか調べます (!fail() の同義語です)
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
状態フラグを返します
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
状態フラグを設定します
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
状態フラグを変更します
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
書式
書式情報をコピーします
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
フィル文字を管理します
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
その他
例外マスクを管理します
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
ロケールを設定します
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
紐付けられているストリームバッファを管理します
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
結び付けられているストリームを管理します
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
文字をナロー化します
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]
文字をワイド化します
(std::basic_ios<CharT,Traits>のパブリックメンバ関数) [edit]

std::ios_base から継承

メンバ関数

書式
書式フラグを管理します
(std::ios_baseのパブリックメンバ関数) [edit]
特定の書式フラグをセットします
(std::ios_baseのパブリックメンバ関数) [edit]
特定の書式フラグをクリアします
(std::ios_baseのパブリックメンバ関数) [edit]
浮動小数点操作の10進精度を管理します
(std::ios_baseのパブリックメンバ関数) [edit]
フィールド幅を管理します
(std::ios_baseのパブリックメンバ関数) [edit]
ロケール
ロケールを設定します
(std::ios_baseのパブリックメンバ関数) [edit]
現在のロケールを返します
(std::ios_baseのパブリックメンバ関数) [edit]
内部の拡張可能な配列
[静的]
pword() および iword() へのインデックスとして使用するのに安全な、プログラム全体で一意な整数を返します
(std::ios_baseのパブリック静的メンバ関数) [edit]
必要であればプライベートな記憶域をリサイズし、指定されたインデックスの long 要素にアクセスします
(std::ios_baseのパブリックメンバ関数) [edit]
必要であればプライベートな記憶域をリサイズし、指定されたインデックスの void* 要素にアクセスします
(std::ios_baseのパブリックメンバ関数) [edit]
その他
イベントのコールバック関数を登録します
(std::ios_baseのパブリックメンバ関数) [edit]
C++ と C の入出力ライブラリが相互運用可能であるかどうかを設定します
(std::ios_baseのパブリック静的メンバ関数) [edit]
メンバクラス
ストリームの例外
(std::ios_baseのパブリックメンバクラス) [edit]
標準ストリームオブジェクトを初期化します
(std::ios_baseのパブリックメンバクラス) [edit]

メンバ型および定数

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

以下の定数が定義されます。

定数 説明[edit]
app 各書き込み前にストリームの終端へシークします[edit]
binary バイナリモードで開きます[edit]
in 読み込み用に開きます[edit]
out 書き込み用に開きます[edit]
trunc 開くときにストリームの内容を破棄します[edit]
ate 開いた直後にストリームの終端へシークします[edit]

(typedef) [edit]
書式フラグの型

以下の定数が定義されます。

定数 説明[edit]
dec 整数の入出力に対して10進数を使用します。 std::dec を参照してください[edit]
oct 整数の入出力に対して8進数を使用します。 std::oct を参照してください[edit]
hex 整数の入出力に対して16進数を使用します。 std::hex を参照してください[edit]
basefield dec|oct|hex。 マスク操作に便利です[edit]
left 左詰め (フィル文字を右に追加します)。 std::left を参照してください [edit]
right 右詰め (フィル文字を左に追加します)。 std::right を参照してください [edit]
internal 内部詰め (フィル文字を内部の指定された位置に追加します)。 std::internal を参照してください [edit]
adjustfield left|right|internal。 マスク操作に便利です [edit]
scientific 科学表記を使用して浮動小数点型を生成します (fixed と組み合わせた場合は16進表記)。 std::scientific を参照してください [edit]
fixed 固定表記を使用して浮動小数点型を生成します (scientific と組み合わせた場合は16進表記)。 std::fixed を参照してください [edit]
floatfield scientific|fixed。 マスク操作に便利です [edit]
boolalpha bool 型をアルファベット形式で挿入および抽出します。 std::boolalpha を参照してください [edit]
showbase 整数の出力に対して数値の基数を表す接頭辞を生成し、金額の入出力に対して金額指示子を要求します。 std::showbase を参照してください [edit]
showpoint 浮動小数点数の出力に対して小数点を無条件に生成します。 std::showpoint を参照してください [edit]
showpos 非負の数値の出力に対して + 文字を出力します。 std::showpos を参照してください [edit]
skipws 特定の入力操作の前に先行するホワイトスペースをスキップします。 std::skipws を参照してください [edit]
unitbuf 各出力操作の後に出力をフラッシュします。 std::unitbuf を参照してください [edit]
uppercase 特定の出力操作で特定の小文字を同等な大文字に置き換えます。 std::uppercase を参照してください [edit]

(typedef) [edit]
ストリーム状態の型

以下の定数が定義されます。

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

(typedef) [edit]
シーク方向の型

以下の定数が定義されます。

定数 説明[edit]
beg ストリームの先頭 [edit]
end ストリームの終端 [edit]
cur ストリームの位置指示子の現在位置 [edit]

(typedef) [edit]
イベントの種類を規定します
(列挙) [edit]
コールバック関数の型
(typedef) [edit]

[編集] 関連項目

入出力ストリームから文字列にデータを読み込みます
(関数) [edit]

[編集]

#include <iostream>
#include <fstream>
#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
    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