命名空间
变体
操作

std::basic_stringbuf<CharT,Traits,Allocator>::seekoff

来自 cppreference.cn
< cpp‎ | io‎ | basic stringbuf
 
 
 
 
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 );

如果可能,将 std::basic_streambuf::gptr 和/或 std::basic_streambuf::pptr 重新定位到缓冲区 get 和/或 put 区域的起始位置、结束位置或当前位置偏移 off 个字符的位置。

如果 gptr 和/或 pptr 被重新定位,则按如下方式完成

1) 确定类型为 off_type 的新指针偏移量 newoff
a) 如果 dir == std::ios_base::beg,则 newoff 为零
b) 如果 dir == std::ios_base::cur,则 newoff 是指针的当前位置(gptr() - eback()pptr() - pbase()
c) 如果 dir == std::ios_base::end,则 newoff 是缓冲区整个初始化部分的长度(如果使用 过度分配,则为高水位线指针减去起始指针)
2) 如果要重新定位的指针是空指针,并且 newoff 为非零,则此函数将失败。
3) 如果 newoff + off < 0(重新定位会将指针移动到缓冲区开头之前),或者如果 newoff + off 将指向缓冲区末尾之后(或者如果使用 过度分配,则指向缓冲区中最后一个初始化的字符之后),则该函数将失败。
4) 否则,指针的赋值方式类似于 gptr() = eback() + newoff + offpptr() = pbase() + newoff + off

目录

[编辑] 参数

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

[编辑] 返回值

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

[编辑] 示例

#include <iostream>
#include <sstream>
 
int main()
{
    std::stringstream ss("123"); // in/out
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
 
    // absolute positioning both pointers
    ss.rdbuf()->pubseekoff(1, std::ios_base::beg); // move both 1 forward
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
 
    // try to move both pointers 1 forward from current position
    if (-1 == ss.rdbuf()->pubseekoff(1, std::ios_base::cur))
        std::cout << "moving both pointers from current position failed\n";
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
 
    // move the write pointer 1 forward, but not the read pointer
    // can also be called as 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'; // write at put position
    std::cout << "Wrote 'a' at put position, the buffer is now " << ss.str() << '\n';
 
    char ch;
    ss >> ch;
    std::cout << "reading at get position gives '" << 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++ 标准。

DR 应用于 已发布行为 正确行为
LWG 55 C++98 seekoff 返回未定义的
失败时的无效流位置
pos_type(off_type(-1))
失败时返回
LWG 375 C++98 std::ios_base 的静态常量成员是
错误地指定为 std::basic_ios 的成员
已更正
LWG 432 C++98 即使 newoff + off 也可能成功 seekoff
将指向最后一个初始化的字符之后
seekoff 失败
在这种情况下
LWG 453 C++98 重新定位空 gptr() 和/或空 pptr()
新偏移量为零时总是失败
在这种情况下它可以成功
LWG 563 C++98 结束指针不能用于计算 newoff,因为它可能
在解决 LWG issue 432 后无法被程序精确控制
使用高水位线
指针代替

[编辑] 参见

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