名前空間
変種
操作

std::strstreambuf::seekpos

From cppreference.com
< cpp‎ | io‎ | strstreambuf
 
 
 
 
protected:

virtual pos_type seekpos( pos_type sp,
                          std::ios_base::openmode によって =

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

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

whichstd::ios_base::in が設定されている場合、gptr() (get領域の次のポインタ) を移動しようとします。whichstd::ios_base::out が設定されている場合、pptr() (put領域の次のポインタ) を移動しようとします。which にどちらのビットも設定されていない場合、操作は失敗します。

各次のポインタは以下のように移動されます。

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

目次

[編集] パラメータ

sp - ストリーム位置。例えば seekoff() または seekpos() によって取得されたもの。
which - 入力シーケンス、出力シーケンス、またはその両方に影響するかどうかを定義します。これは、次の定数のいずれか、またはそれらの組み合わせです。
Constant 説明
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> の仮想 protected メンバ関数) [編集]
[virtual]
入力シーケンス、出力シーケンス、またはその両方で、絶対アドレス指定を使用して次のポインタを再配置する
(virtual protected member function of std::basic_stringbuf<CharT,Traits,Allocator>) [編集]
[virtual]
絶対アドレスを使用してファイル位置を再配置します
(virtual protected member function of std::basic_filebuf<CharT,Traits>) [編集]
English 日本語 中文(简体) 中文(繁體)