Namespaces
Variants

std::basic_stringbuf<CharT,Traits,Allocator>:: seekoff

From cppreference.net
protected :

virtual pos_type seekoff ( off_type off,
std:: ios_base :: seekdir dir,

std:: ios_base :: openmode which = std:: ios_base :: in | std:: ios_base :: out ) ;

std::basic_streambuf::gptr および/または std::basic_streambuf::pptr を、可能であればバッファのget領域および/またはput領域の先頭、末尾、または現在位置から正確に off 文字分の位置に再配置します。

gptr および/または pptr が再配置される場合、以下のように行われます:

1) 新しいポインタオフセット newoff は型 off_type として決定されます
a) if dir == std:: ios_base :: beg 、ならば newoff はゼロである
b) if dir == std:: ios_base :: cur 、その場合 newoff はポインタの現在位置( gptr ( ) - eback ( ) または pptr ( ) - pbase ( ) )となる
c) if dir == std:: ios_base :: end 、その場合 newoff はバッファの初期化済み部分全体の長さとなる( オーバーアロケーション が使用されている場合、ハイウォーターマークポインタから先頭ポインタを減算した値)
2) 再配置対象のポインタがヌルポインタであり、 newoff が非ゼロである場合、この関数は失敗します。
3) もし newoff + off < 0 (再配置によってポインタがバッファの先頭より前へ移動する場合)、または newoff + off がバッファの終端を超える位置を指す場合(または over-allocation が使用されている場合、バッファ内の最後に初期化された文字を超える場合)、関数は失敗します。
4) それ以外の場合、ポインタは以下のように割り当てられる gptr ( ) = eback ( ) + newoff + off または pptr ( ) = pbase ( ) + newoff + off

目次

パラメータ

off - 次のポインタの位置を設定する相対位置
dir - 相対オフセットを適用する基準位置を定義します。以下の定数のいずれかを指定できます:
定数 説明
beg ストリームの先頭
end ストリームの末尾
cur ストリーム位置指示子の現在位置
which - 入力シーケンス、出力シーケンス、またはその両方が影響を受けるかどうかを定義します。以下の定数のいずれか、または組み合わせを指定できます:
定数 説明
in 入力シーケンスに影響
out 出力シーケンスに影響

戻り値

pos_type ( newoff ) 成功時、 pos_type ( off_type ( - 1 ) ) 失敗時、または pos_type が結果のストリーム位置を表現できない場合。

#include <iostream>
#include <sstream>
int main()
{
    std::stringstream ss("123"); // 入出力
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
    // 両方のポインタを絶対位置指定
    ss.rdbuf()->pubseekoff(1, std::ios_base::beg); // 両方を1つ前に移動
    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'; // put位置に書き込み
    std::cout << "put位置に'a'を書き込み、バッファは現在 " << ss.str() << '\n';
    char ch;
    ss >> ch;
    std::cout << "get位置での読み取り結果は '" << 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++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 55 C++98 seekoff は失敗時に未定義の
無効なストリーム位置を返した
pos_type ( off_type ( - 1 ) )
が失敗時に返される
LWG 375 C++98 std::ios_base の静的定数メンバが
std::basic_ios のメンバとして誤って指定されていた
修正された
LWG 432 C++98 seekoff newoff + off が最後の初期化された
文字を超える位置を指す場合でも成功する可能性があった
seekoff はこの場合
失敗する
LWG 453 C++98 nullの gptr ( ) および/または nullの pptr ( )
オフセットゼロで再配置すると常に失敗した
この場合は成功することがある
LWG 563 C++98 終端ポインタは LWG issue 432 を解決した後、
プログラムで正確に制御できなかったため newoff の計算に使用できなかった
代わりにハイ・ウォーターマーク
ポインタを使用する

関連項目

seekoff を呼び出す
( std::basic_streambuf<CharT,Traits> のpublicメンバ関数)
[virtual]
絶対アドレス指定を使用して、入力シーケンス、出力シーケンス、または両方の次のポインタを再配置する
(仮想protectedメンバ関数)
[virtual]
相対アドレス指定を使用してファイル位置を再配置する
( std::basic_filebuf<CharT,Traits> の仮想protectedメンバ関数)
[virtual]
相対アドレス指定を使用して、入力シーケンス、出力シーケンス、または両方の次のポインタを再配置する
( std::strstreambuf の仮想protectedメンバ関数)