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++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
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> 的虚拟保护成员函数) |