std::strstreambuf::seekoff
来自 cppreference.cn
< cpp | io | strstreambuf
protected: virtual pos_type seekoff( off_type off, |
(在 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
的新指针偏移量 newoffa) 如果 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 | - | 定义应用相对偏移量的基准位置。它可以是以下常量之一
| ||||||||
which | - | 定义输入序列、输出序列或两者是否受影响。它可以是以下常量之一或组合
|
[编辑] 返回值
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); // 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 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: '"; std::cout.write(a, sizeof a); std::cout << "'\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)) 失败时返回 |
[编辑] 参见
[虚拟] |
使用绝对寻址重新定位输入序列、输出序列或两者中的下一个指针 (虚拟受保护成员函数) |
[虚拟] |
使用相对寻址重新定位输入序列、输出序列或两者中的下一个指针 ( std::basic_streambuf<CharT,Traits> 的虚拟受保护成员函数) |
[虚拟] |
使用相对寻址重新定位输入序列、输出序列或两者中的下一个指针 ( std::basic_stringbuf<CharT,Traits,Allocator> 的虚拟受保护成员函数) |
[虚拟] |
使用相对寻址重新定位文件位置 ( std::basic_filebuf<CharT,Traits> 的虚拟受保护成员函数) |