命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | io‎ | basic filebuf
 
 
 
 
protected:

virtual pos_type seekoff( off_type off,
                          std::ios_base::seekdir dir,

                          std::ios_base::openmode which = std::ios_base::in | std::ios_base::out );

如果可能,将文件指针重新定位到与从文件开头、结尾或当前位置(取决于 dir 的值)偏移量恰好为 off 个字符的位置。

如果关联的文件未打开 (is_open() == false),则立即失败。

如果多字节字符编码是状态相关的 (codecvt::encoding() 返回 -1) 或可变长度的 (codecvt::encoding() 返回 0) 并且偏移量 off 不为 0,则立即失败:此函数无法确定与 off 个字符对应的字节数。

如果 dir 不是 std::basic_ios::cur 或偏移量 off 不为 0,并且在此 filebuf 对象上完成的最近一次操作是输出(即,put 缓冲区不为空,或者最近调用的函数是 overflow()),则调用 std::codecvt::unshift 以确定必要的回退序列,并通过调用 overflow() 将该序列写入文件。

然后将参数 dir 转换为 int 类型的 whence 值,如下所示

dir 的值 whence 的值
std::basic_ios::beg SEEK_SET
std::basic_ios::end SEEK_END
std::basic_ios::cur SEEK_CUR

然后,如果字符编码是固定宽度的 (codecvt::encoding() 返回某个正数 width),则移动文件指针,如同通过 std::fseek(file, width*off, whence)

否则,移动文件指针,如同通过 std::fseek(file, 0, whence)

基类函数签名所需的 openmode 参数通常被忽略,因为 std::basic_filebuf 仅维护一个文件位置。

内容

[编辑] 参数

off - 用于设置位置指示器的相对位置
dir - 定义应用相对偏移量的基准位置。它可以是以下常量之一
常量 解释
beg 流的开始
end 流的结尾
cur 流位置指示器的当前位置
which - 定义要影响的输入和/或输出序列。它可以是以下常量之一或组合
常量 解释
in 影响输入序列
out 影响输出序列

[编辑] 返回值

新构造的 pos_type 类型的对象,它存储结果文件位置,如果失败,则返回 pos_type(off_type(-1))

[编辑] 注释

seekoff()std::basic_streambuf::pubseekoff 调用,后者由 std::basic_istream::seekg, std::basic_ostream::seekp, std::basic_istream::tellg, 和 std::basic_ostream::tellp 调用。

[编辑] 示例

#include <fstream>
#include <iostream>
#include <locale>
 
template<typename CharT>
int get_encoding(const std::basic_istream<CharT>& stream)
{
    using Facet = std::codecvt<CharT, char, std::mbstate_t>;
    return std::use_facet<Facet>(stream.getloc()).encoding();
}
 
int main()
{
    // prepare a 10-byte file holding 4 characters ("zß水𝄋") in UTF-8
    std::ofstream("text.txt") << "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";
 
    // open using a non-converting encoding
    std::ifstream f1("text.txt");
    std::cout << "f1's locale's encoding() returns "
              << get_encoding(f1) << '\n'
              << "pubseekoff(3, beg) returns "
              << f1.rdbuf()->pubseekoff(3, std::ios_base::beg) << '\n'
              << "pubseekoff(0, end) returns "
              << f1.rdbuf()->pubseekoff(0, std::ios_base::end) << '\n';
 
    // open using UTF-8
    std::wifstream f2("text.txt");
    f2.imbue(std::locale("en_US.UTF-8"));
    std::cout << "f2's locale's encoding() returns "
              << get_encoding(f2) << '\n'
              << "pubseekoff(3, beg) returns "
              << f2.rdbuf()->pubseekoff(3, std::ios_base::beg) << '\n'
              << "pubseekoff(0, end) returns "
              << f2.rdbuf()->pubseekoff(0, std::ios_base::end) << '\n';
}

输出

f1's locale's encoding() returns 1
pubseekoff(3, beg) returns 3
pubseekoff(0, end) returns 10
f2's locale's encoding() returns 0
pubseekoff(3, beg) returns -1
pubseekoff(0, end) returns 10

[编辑] 缺陷报告

以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 55 C++98 seekoff 返回未定义的
失败时的无效流位置
pos_type(off_type(-1))
失败时返回

[编辑] 参见

调用 seekoff()
(std::basic_streambuf<CharT,Traits> 的公共成员函数) [编辑]
[虚拟]
使用绝对寻址重新定位文件位置
(虚拟保护成员函数) [编辑]
将文件位置指示器移动到文件中的特定位置
(函数) [编辑]