Namespaces
Variants

std::basic_ostream<CharT,Traits>:: seekp

From cppreference.net
basic_ostream & seekp ( pos_type pos ) ;
(1)
basic_ostream & seekp ( off_type off, std:: ios_base :: seekdir dir ) ;
(2)

设置当前关联 streambuf 对象的输出位置指示器。

行为表现为 UnformattedOutputFunction (但实际不执行输出操作)。在构造并检查哨兵对象后,

(since 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 - 定义应用相对偏移的基准位置。可以是以下常量之一:
常量 说明
beg 流的起始位置
end 流的结束位置
cur 流位置指示器的当前位置

返回值

* this

异常

1,2) 若发生失败且 exceptions ( ) & failbit ! = 0 时,可能抛出 std::ios_base::failure

示例

#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 针对重载(2)的 LWG 129 问题解决方案被移除 已恢复

参见

返回输出位置指示器
(公开成员函数)
返回输入位置指示器
( std::basic_istream<CharT,Traits> 的公开成员函数)
设置输入位置指示器
( std::basic_istream<CharT,Traits> 的公开成员函数)