std::basic_ostream<CharT,Traits>::seekp
来自 cppreference.cn
< cpp | io | basic_ostream
basic_ostream& seekp( pos_type pos ); |
(1) | |
basic_ostream& seekp( off_type off, std::ios_base::seekdir dir ); |
(2) | |
设置当前关联的 streambuf
对象的输出位置指示器。
行为类似于 UnformattedOutputFunction(除了实际上不执行输出)。在构造并检查哨兵对象之后: |
(C++11 起) |
1) 如果 fail() != true,通过调用 rdbuf()->pubseekpos(pos, std::ios_base::out) 将输出位置指示器设置为绝对(相对于文件开头)值 pos。如果失败,则调用 setstate(std::ios_base::failbit)。
2) 如果 fail() != true,通过调用 rdbuf()->pubseekoff(off, dir, std::ios_base::out) 将输出位置指示器设置为相对于 dir 的偏移量 off。如果失败,则调用 setstate(std::ios_base::failbit)。
目录 |
[编辑] 参数
pos | - | 要设置的输出位置指示器的绝对位置 | ||||||||
off | - | 要设置的输出位置指示器的相对位置(正或负) | ||||||||
dir | - | 定义应用相对偏移量的基本位置。它可以是以下常量之一
|
[编辑] 返回值
*this
[编辑] 异常
[编辑] 示例
运行此代码
#include <iostream> #include <sstream> int main() { std::ostringstream os("hello, world"); os.seekp(7); os << 'W'; os.seekp(0, std::ios_base::end); os << '!'; os.seekp(0); os << 'H'; std::cout << os.str() << '\n'; }
输出
Hello, World!
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 129 | C++98 | 没有办法指示失败 | 失败时设置 failbit |
LWG 136 | C++98 | seekp 可能会设置输入流 |
仅设置输出流 |
LWG 537 | C++98 | 1. pos 的类型为 pos_type& 2. off 的类型为 off_type& |
1. 修正为 pos_type 2. 修正为 off_type |
LWG 2341 | C++98 | 已移除 LWG issue 129 对重载 (2) 的解决方案 | 恢复 |
[编辑] 参见
返回输出位置指示器 (公有成员函数) | |
返回输入位置指示符 ( std::basic_istream<CharT,Traits> 的公有成员函数) | |
设置输入位置指示符 ( std::basic_istream<CharT,Traits> 的公有成员函数) |