定义于头文件 <fstream>
| template<      class CharT,  | 
std::basic_filebuf 是关联字符序列为文件的 std::basic_streambuf 。输入序列和输出序列都关联到同一文件,并为两种操作维护连接文件位置。
函数 underflow() 和 overflow()/sync() 进行文件和缓冲区的获取放置区之间的实际 I/O 。 CharT 不是 char 时,多数实现在文件存储多字节字符,并用 std::codecvt 平面进行宽/多字节字符转换。
亦为常用字符类型定义二个特化:
| 类型 | 定义 | 
filebuf | basic_filebuf<char> | 
wfilebuf | basic_filebuf<wchar_t> | 
受保护成员函数
用绝对寻址重寻位文件位置
std::basic_filebuf<CharT,Traits>::seekpos 
| protected:  virtual pos_type seekpos( pos_type sp, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out ); | 
若可能,则重寻位文件指针到 sp 所指示的位置。
若关联文件未打开( is_open()==false ),则立即失败。
若文件为写入打开,则首先用 overflow() 写入放置区和任何当前感染的 locale 所要求的反迁移序列。
然后如同通过调用 std::fsetpos() 重寻位指针。
若文件为读取打开,则若需要则更新获取区。
若 sp 不是由在同一文件上调用 seekoff() 或 seekpos() 获得,则行为未定义。
参数
| sp | - | 之前在同一文件上调用 seekoff() 或 seekpos() 获得的文件位置 | ||||||
| which | - | 确定要影响输入和/或输出序列的哪个。它能为下列常量之一或其组合: 
    
  | 
返回值
成功时为 sp ,失败时为 pos_type(off_type(-1)) 。
注意
seekpos() 为 std::basic_streambuf::pubseekpos() 所调用,后者为 std::basic_istream::seekg() 和 std::basic_ostream::seekp() 的单参数版本调用。
许多实现不于 seekpos() 中更新获取区,而是委托给下次 sgetc() 所调用的 underflow() 。
调用示例
#include <fstream>
#include <iostream>
struct mybuf : std::filebuf
{
    pos_type seekpos(pos_type sp, std::ios_base::openmode which)
    {
        std::cout << "Before seekpos(" << sp << "), size of the get area is "
                  << egptr() - eback() << " with "
                  << egptr() - gptr() << " read positions available\n";
        pos_type rc = std::filebuf::seekpos(sp, which);
        std::cout << "seekpos() returns " << rc << ".\nAfter the call, "
                  << "size of the get area is "
                  << egptr() - eback() << " with "
                  << egptr() - gptr() << " read positions available\n";
// 若 seekpos() 清空获取区则反注释
//         std::filebuf::underflow();
//         std::cout << "after forced underflow(), size of the get area is "
//                   << egptr()-eback() << " with "
//                   << egptr()-gptr() << " read positions available\n";
        return rc;
    }
};
int main()
{
    mybuf buf;
    buf.open("test.txt", std::ios_base::in);
    std::istream stream(&buf);
    stream.get(); // 读一个字符以强制 underflow()
    stream.seekg(2);
} 
输出

从放置区写字符到关联文件
std::basic_filebuf<CharT,Traits>::sync 
|   protected:  | 
若放置区存在(例如文件为写入打开),则调用 overflow() 写入所有未处理输出到文件,然后如同以调用 std::fflush 冲入文件。
若获取区存在(例如文件为读取打开),则效果是实现定义的。典型实现可能清空获取区,并将当前文件位置后移对应的字节数。
参数
(无)
返回值
成功情况下为 0 ,失败情况下为 -1 。
注意
对于输出流, close() 、 seekoff() 和 seekpos() 隐式调用,而 std::basic_streambuf::pubsync() 显式调用 sync() 或其等价内容。
更改关联的本地环境
std::basic_filebuf<CharT,Traits>::imbue 
|   protected:  | 
更改关联的本地环境,使得此调用后(和另一 imbue() 调用前)所有插入或释出的字符以 loc 的 std::codecvt 平面转换。
若旧 locale 的编码依赖状态,且文件未被寻位到起始,则新的 locale 必须与先前感染者拥有相同的 std::codecvt 平面。
参数
| loc | - | 要与流感染的 locale | 
返回值
(无)
非成员函数
特化 std::swap 算法
std::swap(std::basic_filebuf) 
| template< class CharT, class Traits >  void swap( std::basic_filebuf<CharT,Traits>& lhs, std::basic_filebuf<CharT,Traits>& rhs ); | (C++11 起) | 
 为 std::basic_filebuf 特化 std::swap 算法。交换 lhs 与 rhs 的内部状态。等效地调用 lhs.swap(rhs) 。
参数
| lhs, rhs | - | 要交换状态的 std::basic_filebuf 对象 | 
返回值
(无)














![golangd\pycharm-ai免费代码助手安装使用gpt4-免费使用--[推荐]](https://img-blog.csdnimg.cn/5c495b2d60d441d0a337152c54086d14.png)




