名前空間
変種
操作

std::strstreambuf::seekoff

提供: cppreference.com
< cpp‎ | io‎ | strstreambuf
 
 
入出力ライブラリ
入出力マニピュレータ
Cスタイルの入出力
バッファ
(C++98で非推奨)
ストリーム
抽象
ファイル入出力
文字列入出力
配列入出力
(C++98で非推奨)
(C++98で非推奨)
(C++98で非推奨)
同期化出力
エラーカテゴリインタフェース
(C++11)
 
 
protected:

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

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

可能であれば、 std::basic_streambuf::gptr または std::basic_streambuf::pptr または両方の位置を、バッファの get 領域または put 領域の先頭、終端、現在位置からちょうど off 文字に対応する位置に、再設定します。

  • whichios_base::in を含み、このバッファが読み込み用に開かれている場合は、下で説明する通りに get 領域内の読み込みポインタ std::basic_streambuf::gptr の位置が再設定します。
  • whichios_base::out を含み、このバッファが書き込み用に開かれている場合は、下で説明する通りに put 領域内の書き込みポインタ std::basic_streambuf::pptr の位置を再設定します。
  • whichios_base::inios_base::out をどちらも含み、バッファが読み書き両用に開かれており、 whenios_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 - 相対位置を設定するためのベースの位置を定義します。 以下の定数のいずれかを指定できます。
定数 説明
beg ストリームの先頭
end ストリームの終端
cur ストリームの位置指示子の現在位置
which - 入力シーケンス、出力シーケンス、両方のいずれが影響を受けるかを定義します。 以下の定数のいずれかまたは組み合わせを指定できます。
定数 説明
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'

[編集] 関連項目

[仮想]
入力シーケンス、出力シーケンス、またはその両方の次ポインタの位置を絶対位置を使用して再設定します
(std::basic_streambuf<CharT,Traits>の仮想プロテクテッドメンバ関数) [edit]
[仮想]
入力シーケンス、出力シーケンス、またはその両方の次ポインタの位置を相対位置を使用して再設定します
(std::basic_streambuf<CharT,Traits>の仮想プロテクテッドメンバ関数) [edit]
[仮想]
入力シーケンス、出力シーケンス、または両方の次ポインタの位置を、相対位置を用いて再設定します
(std::basic_stringbuf<CharT,Traits,Allocator>の仮想プロテクテッドメンバ関数) [edit]
[仮想]
相対位置を使用してファイル位置を再設定します
(std::basic_filebuf<CharT,Traits>の仮想プロテクテッドメンバ関数) [edit]