std:: basic_ofstream
|
ヘッダーで定義
<fstream>
|
||
|
template
<
class
CharT,
|
||
クラステンプレート
basic_ofstream
は、ファイルベースのストリームに対する高レベルな出力操作を実装します。これはファイルベースのストリームバッファ(
std::basic_filebuf
)を高レベルインターフェース(
std::basic_ostream
)と連携させます。
std::basic_ofstream
の典型的な実装は、派生していないデータメンバを1つだけ保持します:
std::
basic_filebuf
<
CharT, Traits
>
のインスタンスです。
継承図
一般的な文字型に対するいくつかの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) |
|
|
(C++26)
|
基盤となる実装定義のハンドルを返す
(public member function) |
ファイル操作 |
|
|
ストリームに関連付けられたファイルがあるかどうかをチェックする
(public member function) |
|
|
ファイルを開き、ストリームに関連付ける
(public member function) |
|
|
関連付けられたファイルを閉じる
(public member function) |
|
非メンバー関数
|
(C++11)
|
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メンバ関数)
|
|
|
[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.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