名前空間
変種
操作

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

From cppreference.com
< cpp‎ | io‎ | basic stringbuf
 
 
 
 
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 );

バッファの取得領域および/または書き込み領域の、先頭、末尾、または現在の位置からちょうど off 文字に対応する位置に、可能であれば std::basic_streambuf::gptr および/または std::basic_streambuf::pptr を再配置します。

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

1) 新しいポインタオフセット newoffoff_type 型)が決定されます。
a) もし dir == std::ios_base::beg ならば、newoff はゼロです。
b) もし dir == std::ios_base::cur ならば、newoff はポインタの現在の位置です(gptr() - eback() または pptr() - pbase())。
c) もし dir == std::ios_base::end ならば、newoff はバッファの初期化された部分全体の長さです(オーバーアロケーションが使用されている場合、高水位ポインタから先頭ポインタを引いた値)。
2) 再配置されるポインタがヌルポインタであり、かつ newoff がゼロ以外になる場合、この関数は失敗します。
3) もし newoff + off < 0(再配置によりポインタがバッファの先頭より前に移動する場合)または newoff + off がバッファの末尾(または オーバーアロケーションが使用されている場合は、初期化された最後の文字以降)を越える場合、関数は失敗します。
4) それ以外の場合、ポインタは gptr() = eback() + newoff + off または pptr() = pbase() + newoff + off のように代入されます。

目次

[編集] パラメータ

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

[編集] 戻り値

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

[編集]

#include <iostream>
#include <sstream>
 
int main()
{
    std::stringstream ss("123"); // 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 1 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 " << ss.str() << '\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))
が失敗時に返される
LWG 375 C++98 std::ios_base の静的定数メンバが
std::basic_ios のメンバとして誤って指定されていた
修正済み
LWG 432 C++98 seekoffnewoff + off
初期化された最後の文字を越える位置を指す場合でも成功する可能性があった
seekoff は失敗する
この場合に
LWG 453 C++98 ヌルの gptr() および/またはヌルの pptr() の再配置
オフセットゼロで常に失敗していた
この場合、成功する可能性がある
LWG 563 C++98 newoff を計算するために末尾ポインタを使用できなかった。なぜなら、LWG issue 432 を解決した後、プログラムによって正確に制御できなかったため。
代わりに高水位ポインタを使用する
ポインタを使用する
ポインタを使用する

[編集] 関連項目

seekoff() を呼び出す
(std::basic_streambuf<CharT,Traits> の public メンバ関数) [編集]
[virtual]
入力シーケンス、出力シーケンス、またはその両方で、絶対アドレス指定を使用して次のポインタを再配置する
(仮想protectedメンバ関数) [編集]
[virtual]
相対アドレスを使用してファイル位置を再配置します
(virtual protected member function of std::basic_filebuf<CharT,Traits>) [編集]
[virtual]
入力シーケンス、出力シーケンス、またはその両方で、相対アドレス指定を使用して次のポインタを再配置する
(virtual protected member function of std::strstreambuf) [編集]
English 日本語 中文(简体) 中文(繁體)