Namespaces
Variants

std:: basic_ofstream

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

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

> class basic_ofstream : public std:: basic_ostream < CharT, Traits >

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

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

cpp/io/ios base cpp/io/basic ios cpp/io/basic ostream std-basic ofstream-inheritance.svg

継承図

一般的な文字型に対するいくつかのtypedefが提供されています:

定義済みヘッダー <fstream>
定義
std::ofstream std :: basic_ofstream < char >
std::wofstream std :: basic_ofstream < 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)
(destructor)
[virtual] (implicitly declared)
basic_ofstream と関連するバッファを破棄し、ファイルを閉じる
(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_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.b";
    {
        std::ofstream ostrm(filename, std::ios::binary);
        double d = 3.14;
        ostrm.write(reinterpret_cast<char*>(&d), sizeof d); // バイナリ出力
        ostrm << 123 << "abc" << '\n';                      // テキスト出力
    }
    // 読み戻し
    std::ifstream istrm(filename, std::ios::binary);
    double d;
    istrm.read(reinterpret_cast<char*>(&d), sizeof d);
    int n;
    std::string s;
    istrm >> n >> s;
    std::cout << " read back: " << d << ' ' << n << ' ' << s << '\n';
}

出力:

read back: 3.14 123 abc