namespace std {
template<class CharT, class Traits = char_traits<CharT>>
class basic_filebuf : public basic_streambuf<CharT, Traits> { …… };
using filebuf = basic_filebuf<char>;
using wfilebuf = basic_filebuf<wchar_t>;
}
概要
basic_filebufは、ファイルに対するストリームバッファを表現するクラスである。
CのFILE*に対する入出力関数を使って実装される。
位置操作(seekoffとseekpos)は、入力・出力双方で共有される。
テンプレートパラメータとして文字型を受け取るようになっており、使用を容易にするため、以下のパラメータ設定済みエイリアスが定義されている。
このエイリアスは<fstream>ヘッダと<iosfwd>ヘッダで定義されている。
| エイリアス | 説明 | 対応バージョン |
|---|---|---|
filebuf |
char型。ASCII、UTF-8等のマルチバイト文字列や、バイナリデータとして使用する。 |
|
wfilebuf |
wchar_t型。ワイド文字列として使用する。 |
メンバ関数
構築・破棄
| 名前 | 説明 | 対応バージョン |
|---|---|---|
(constructor) |
コンストラクタ | |
(destructor) |
デストラクタ | |
operator= |
ムーブ代入 | C++11 |
swap |
値の交換 | C++11 |
- コピーコンストラクタとコピー代入演算子は
delete宣言されている。
オープンとクローズ
| 名前 | 説明 | 対応バージョン |
|---|---|---|
is_open |
開いている状態であることの判定 | |
open |
ファイルを開く | |
close |
ファイルを閉じる |
環境固有の情報
| 名前 | 説明 | 対応バージョン |
|---|---|---|
native_handle() |
ネイティブハンドルを取得する[処理系定義] | C++26 |
オーバーライドされている関数
すべてprotectedで定義されている。
| 名前 | 説明 | 対応バージョン |
|---|---|---|
imbue |
ロケールを設定する | |
setbuf |
バッファ領域を与える | |
seekoff |
相対指定での位置移動 | |
seekpos |
絶対指定での位置移動 | |
sync |
出力列の同期 | |
uflow |
ファイルから文字を読み込み、読み取り位置を進める | |
underflow |
ファイルから文字を読み込む | |
showmanyc |
ブロックせずに読み取れると期待される文字数を得る | |
pbackfail |
1文字を入力列に戻す | |
overflow |
蓄えられた文字をファイルへ書き出す |
非メンバ関数
| 名前 | 説明 | 対応バージョン |
|---|---|---|
swap |
2つのオブジェクトの値を交換する | C++11 |
メンバ型
| 名前 | 説明 | 対応バージョン |
|---|---|---|
char_type |
テンプレート仮引数CharT |
|
int_type |
Traits::int_type |
|
pos_type |
Traits::pos_type |
|
off_type |
Traits::off_type |
|
traits_type |
テンプレート仮引数Traits |
|
native_handle_type |
ネイティブハンドルの型 [処理系定義] | C++26 |
例
#include <iostream>
#include <fstream>
int main()
{
// ファイルへ書き込む
{
std::filebuf buf;
buf.open("test.txt", std::ios_base::out);
buf.sputn("Hello", 5);
}
// ファイルから読み込む
std::filebuf buf;
buf.open("test.txt", std::ios_base::in);
for (std::filebuf::int_type c = buf.sbumpc();
c != std::filebuf::traits_type::eof();
c = buf.sbumpc()) {
std::cout << static_cast<char>(c);
}
std::cout << std::endl;
}
出力
Hello
バージョン
言語
- C++98