名前空間
変種
操作

std::strstreambuf::seekoff

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

virtual pos_type seekoff( off_type off,
                          ios_base::seekdir way,

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

バッファの入力領域および/または出力領域における、先頭、末尾、または現在位置からちょうど off 文字に対応する位置に、可能であれば std::basic_streambuf::gptr および/または std::basic_streambuf::pptr を移動します。

  • whichios_base::in を含み、このバッファが読み込み用に開かれている場合、読み取りポインタ std::basic_streambuf::gptr を、下記のように入力領域内で移動します。
  • whichios_base::out を含み、このバッファが出力用に開かれている場合、書き込みポインタ std::basic_streambuf::pptr を、下記のように出力領域内で移動します。
  • whichios_base::inios_base::out の両方を含み、バッファが読み書き両用に開かれており、かつ wayios_base::beg または ios_base::end のいずれかである場合、読み取りポインタと書き込みポインタの両方を下記のように移動します。
  • それ以外の場合、この関数は失敗します。

ポインタ(gptr または pptr、あるいはその両方)が移動される場合、それは以下のように行われます。

1) 移動されるポインタがヌルポインタであり、かつ新しいオフセット newoff がゼロでない場合、この関数は失敗します。
2) `off_type` 型の新しいポインタオフセット newoff が決定されます。
a) way == ios_base::beg の場合、newoff はゼロです。
b) way == ios_base::cur の場合、newoff はポインタの現在の位置です(gptr() - eback() または pptr() - pbase())。
c) way == ios_base::end の場合、newoff はバッファの初期化された部分全体の長さです(オーバーアロケーションが使用されている場合、高水マークポインタから開始ポインタを引いた値)。
3) newoff + off が負であるか、またはバッファの初期化された部分の範囲外である場合、関数は失敗します。
4) それ以外の場合、ポインタは gptr() = eback() + newoff + off または pptr() = pbase() + newoff + off によって代入されるかのように設定されます。

目次

[編集] パラメータ

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

[編集] 戻り値

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

[編集]

#include <iostream>
#include <strstream>
 
int main()
{
    char a[] = "123";
    std::strstream ss(a, sizeof a); // in/out
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
 
    // absolute positioning both pointers
    ss.rdbuf()->pubseekoff(1, std::ios_base::beg); // move both forward
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
 
    // try to move both pointers 1 forward from current position
    if (-1 == ss.rdbuf()->pubseekoff(1, std::ios_base::cur))
        std::cout << "moving both pointers from current position failed\n";
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
 
    // move the write pointer 1 forward, but not the read pointer
    // can also be called as 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'; // write at put position
    std::cout << "Wrote 'a' at put position, the buffer is now: '";
    std::cout.write(a, sizeof a);
    std::cout << "'\n";
 
    char ch;
    ss >> ch;
    std::cout << "reading at get position gives '" << 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))
が失敗時に返される

[編集] 関連項目

[virtual]
入力シーケンス、出力シーケンス、またはその両方で、絶対アドレス指定を使用して次のポインタを再配置する
(virtual protected member function) [編集]
[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 日本語 中文(简体) 中文(繁體)