std::strstreambuf:: seekoff
|
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
或两者)被重新定位,其操作方式如下:
off_type
的新指针偏移量
newoff
目录 |
参数
| 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); // 输入/输出 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>
的虚函数保护成员函数)
|