- fstream[meta header]
- std[meta namespace]
- class template[meta id-type]
- filebuf,wfilebuf[meta alias]
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>;
}
- char_traits[link ../string/char_traits.md]
- basic_streambuf[link ../streambuf/basic_streambuf.md]
basic_filebufは、ファイルに対するストリームバッファを表現するクラスである。
CのFILE*に対する入出力関数を使って実装される。
位置操作(seekoffとseekpos)は、入力・出力双方で共有される。
テンプレートパラメータとして文字型を受け取るようになっており、使用を容易にするため、以下のパラメータ設定済みエイリアスが定義されている。
このエイリアスは<fstream>ヘッダと<iosfwd>ヘッダで定義されている。
| エイリアス |
説明 |
対応バージョン |
filebuf |
char型。ASCII、UTF-8等のマルチバイト文字列や、バイナリデータとして使用する。 |
|
wfilebuf |
wchar_t型。ワイド文字列として使用する。 |
|
- コピーコンストラクタとコピー代入演算子は
delete宣言されている。
すべてprotectedで定義されている。
| 名前 |
説明 |
対応バージョン |
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;
}
- std::filebuf[color ff0000]
- buf.open[link basic_filebuf/open.md]
- buf.sputn[link /reference/streambuf/basic_streambuf/sputn.md]
- buf.sbumpc()[link /reference/streambuf/basic_streambuf/sbumpc.md]
- traits_type::eof()[link /reference/string/char_traits/eof.md]