Namespaces
Variants

std::strstreambuf:: seekpos

From cppreference.net
protected :

virtual pos_type seekpos ( pos_type sp,
std:: ios_base :: openmode which =

std:: ios_base :: in | std:: ios_base :: out ) ;
(C++98で非推奨)
(C++26で削除)

std::basic_streambuf::gptr および/または std::basic_streambuf::pptr を、可能であれば sp で示される位置に再配置します。

std::ios_base::in which に設定されている場合、 gptr() (取得領域の次のポインタ)の再配置を試みます。 std::ios_base::out which に設定されている場合、 pptr() (書き込み領域の次のポインタ)の再配置を試みます。 which にどちらのビットも設定されていない場合、操作は失敗します。

各nextポインタは以下のように再配置されます:

  • 次のポインタが null の場合、操作は失敗します。
  • それ以外の場合、新しいオフセット newoff (型 off_type )は sp. offset ( ) を呼び出して決定されます。 newoff が負の値、バッファの範囲外、または無効な場合、操作は失敗します。
  • それ以外の場合、次のポインタは gptr ( ) = eback ( ) + newoff または pptr ( ) = pbase ( ) + newoff によって割り当てられます。

目次

パラメータ

sp - ストリーム位置、 seekoff() または seekpos() によって取得されるもの
which - 入力シーケンス、出力シーケンス、またはその両方が影響を受けるかを定義する。以下の定数の1つまたは組み合わせを指定可能:
定数 説明
in 入力シーケンスに影響
out 出力シーケンスに影響

戻り値

成功時は結果のオフセットを pos_type に変換し、失敗時は pos_type ( off_type ( - 1 ) ) を返します。

注記

seekpos() std::basic_streambuf::pubseekpos() によって呼び出され、これは単一引数バージョンの std::basic_istream::seekg() および std::basic_ostream::seekp() によって呼び出されます。

#include <cstring>
#include <iostream>
#include <strstream>
struct mybuf : std::strstreambuf
{
    mybuf(const char* str) : std::strstreambuf(str, std::strlen(str)) {}
    pos_type seekpos(pos_type sp, std::ios_base::openmode which)
    {
        std::cout << "Before seekpos(" << sp << "), size of the get area is "
                  << egptr() - eback() << " with "
                  << egptr() - gptr() << " read positions available.\n";
        pos_type rc = std::strstreambuf::seekpos(sp, which);
        std::cout << "seekpos() returns " << rc << ".\nAfter the call, "
                  << "size of the get area is "
                  << egptr() - eback() << " with "
                  << egptr() - gptr() << " read positions available.\n";
        return rc;
    }
};
int main()
{
    mybuf buf("12345");
    std::iostream stream(&buf);
    stream.seekg(2);
}

出力:

Before seekpos(2), size of the get area is 5 with 5 read positions available.
seekpos() returns 2.
After the call, size of the get area is 5 with 3 read positions available.

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 55 C++98 seekpos は失敗時に未定義の
無効なストリーム位置を返していた
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> の仮想保護メンバ関数)