名前空間
変種
操作

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

From cppreference.com
< 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 のように動作します(ただし、実際には出力を行いません)。Sentryオブジェクトを構築し、チェックした後、

(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 - 相対オフセットを適用する基準位置を定義します。次のいずれかの定数を使用できます。
Constant 説明
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++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
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 issue 129の解決策が削除された。 復元されました。

[編集] 関連項目

出力位置インジケータを返す
(public member function) [編集]
入力位置インジケータを返す
(std::basic_istream<CharT,Traits> の public メンバ関数) [編集]
入力位置インジケータを設定する
(std::basic_istream<CharT,Traits> の public メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)