c++11 标准模板(STL)(std::basic_filebuf)(八)

std::basic_filebuf是一个与文件关联的缓冲区类,用于输入/输出操作。它管理着文件和缓冲区之间的数据传输,提供了seekpos()函数进行文件位置的绝对重定位,sync()确保缓冲区内容写入文件,以及imbue()改变关联的locale。文章还提到了std::swap的特化版本,用于交换两个基本文件缓冲区的状态。
摘要由CSDN通过智能技术生成

定义于头文件 <fstream>

template<

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

> class basic_filebuf : public std::basic_streambuf<CharT, Traits>

std::basic_filebuf 是关联字符序列为文件的 std::basic_streambuf 。输入序列和输出序列都关联到同一文件,并为两种操作维护连接文件位置。

函数 underflow() 和 overflow()/sync() 进行文件和缓冲区的获取放置区之间的实际 I/O 。 CharT 不是 char 时,多数实现在文件存储多字节字符,并用 std::codecvt 平面进行宽/多字节字符转换。

亦为常用字符类型定义二个特化:

类型定义
filebufbasic_filebuf<char>
wfilebufbasic_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-确定要影响输入和/或输出序列的哪个。它能为下列常量之一或其组合:
常量解释
in影响输入序列
out影响输出序列

返回值

成功时为 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:
virtual int sync()

若放置区存在(例如文件为写入打开),则调用 overflow() 写入所有未处理输出到文件,然后如同以调用 std::fflush 冲入文件。

若获取区存在(例如文件为读取打开),则效果是实现定义的。典型实现可能清空获取区,并将当前文件位置后移对应的字节数。

参数

(无)

返回值

成功情况下为 ​0​ ,失败情况下为 -1 。

注意

对于输出流, close() 、 seekoff() 和 seekpos() 隐式调用,而 std::basic_streambuf::pubsync() 显式调用 sync() 或其等价内容。

 

更改关联的本地环境

std::basic_filebuf<CharT,Traits>::imbue

protected:
virtual void imbue( const std::locale& loc )

更改关联的本地环境,使得此调用后(和另一 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 算法。交换 lhsrhs 的内部状态。等效地调用 lhs.swap(rhs) 。

参数

lhs, rhs-要交换状态的 std::basic_filebuf 对象

返回值

(无)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值