Namespaces
Variants

std::strstreambuf:: seekoff

From cppreference.net
protected :

virtual pos_type seekoff ( off_type off,
ios_base :: seekdir way,

ios_base :: openmode which = ios_base :: in | ios_base :: out ) ;
(C++98 中已弃用)
(C++26 中移除)

如果可能,将 std::basic_streambuf::gptr 和/或 std::basic_streambuf::pptr 重新定位到缓冲区获取区和/或放置区的起始位置、末尾位置或当前位置偏移 off 个字符处。

  • 如果 which 包含 ios_base :: in 且该缓冲区已打开用于读取,则按如下方式重定位获取区域内的读指针 std::basic_streambuf::gptr
  • 如果 which 包含 ios_base :: out 且该缓冲区已打开用于写入,则按如下方式重定位放置区域内的写指针 std::basic_streambuf::pptr
  • 如果 which 同时包含 ios_base :: in ios_base::out ,且缓冲区已同时打开用于读取和写入,并且 way ios_base :: beg ios_base :: end ,则按如下方式同时重定位读指针和写指针。
  • 否则,此函数执行失败。

如果指针( gptr pptr 或两者)被重新定位,其操作方式如下:

1) 若要重新定位的指针是空指针,且新偏移量 newoff 非零,则此函数执行失败。
2) 确定类型为 off_type 的新指针偏移量 newoff
a) 如果 way == ios_base :: beg ,则 newoff 为零
b) 如果 way == ios_base :: cur ,则 newoff 为指针的当前位置( gptr ( ) - eback ( ) pptr ( ) - pbase ( )
c) way == ios_base :: end ,则 newoff 表示缓冲区已初始化部分的整体长度(若采用过量分配策略,则为高水位标记指针与起始指针的差值)
3) 如果 newoff + off 为负数或超出缓冲区初始化部分的范围,则函数执行失败
4) 否则,指针将按照 gptr ( ) = eback ( ) + newoff + off pptr ( ) = pbase ( ) + newoff + off 的方式被赋值

目录

参数

off - 设置下一个指针的相对位置
way - 定义应用相对偏移量的基准位置。可以是以下常量之一:
常量 说明
beg 流的起始位置
end 流的结束位置
cur 流位置指示器的当前位置
which - 定义影响输入序列、输出序列或两者。可以是以下常量之一或其组合:
常量 说明
in 影响输入序列
out 影响输出序列

返回值

pos_type ( newoff ) 成功时返回, pos_type ( off_type ( - 1 ) ) 失败时返回,且当 pos_type 无法表示结果流位置时。

示例

#include <iostream>
#include <strstream>
int main()
{
    char a[] = "123";
    std::strstream ss(a, sizeof a); // 输入/输出
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
    // 绝对定位两个指针
    ss.rdbuf()->pubseekoff(1, std::ios_base::beg); // 同时向前移动
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
    // 尝试从当前位置同时移动两个指针向前1位
    if (-1 == ss.rdbuf()->pubseekoff(1, std::ios_base::cur))
        std::cout << "从当前位置移动两个指针失败\n";
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
    // 移动写入指针向前1位,但不移动读取指针
    // 也可调用为 ss.seekp(1, std::ios_base::cur);
    ss.rdbuf()->pubseekoff(1, std::ios_base::cur, std::ios_base::out);
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
    ss << 'a'; // 在写入位置写入
    std::cout << "在写入位置写入 'a',缓冲区现在为: '";
    std::cout.write(a, sizeof a);
    std::cout << "'\n";
    char ch;
    ss >> ch;
    std::cout << "在读取位置读取得到 '" << ch << "'\n";
}

输出:

put pos = 0 get pos = 0
put pos = 1 get pos = 1
moving both pointers from current position failed
put pos = 1 get pos = 1
put pos = 2 get pos = 1
Wrote 'a' at put position, the buffer is now: '12a'
reading at get position gives '2'

缺陷报告

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

缺陷报告 适用标准 发布时行为 正确行为
LWG 55 C++98 seekoff 在失败时返回未定义的
无效流位置
pos_type ( off_type ( - 1 ) )
在失败时返回该值

参见

[virtual]
使用绝对寻址重新定位输入序列、输出序列或两者中的下一个指针
(虚函数保护成员函数)
[virtual]
使用相对寻址重新定位输入序列、输出序列或两者中的下一个指针
( std::basic_streambuf<CharT,Traits> 的虚函数保护成员函数)
[virtual]
使用相对寻址重新定位输入序列、输出序列或两者中的下一个指针
( std::basic_stringbuf<CharT,Traits,Allocator> 的虚函数保护成员函数)
[virtual]
使用相对寻址重新定位文件位置
( std::basic_filebuf<CharT,Traits> 的虚函数保护成员函数)