std::basic_stringbuf<CharT,Traits,Allocator>::seekoff
From cppreference.com
< cpp | io | basic stringbuf
| protected: virtual pos_type seekoff( off_type off, |
||
バッファの取得領域および/または書き込み領域の、先頭、末尾、または現在の位置からちょうど off 文字に対応する位置に、可能であれば std::basic_streambuf::gptr および/または std::basic_streambuf::pptr を再配置します。
- もし which が std::ios_base::in を含み、このバッファが読み込み用に開かれている場合(すなわち、(which & std::ios_base::in) == std::ios_base::in)、読み込みポインタ std::basic_streambuf::gptr を取得領域内で以下に記述されるように再配置します。
- もし which が std::ios_base::out を含み、このバッファが書き込み用に開かれている場合(すなわち、(which & std::ios_base::out) == std::ios_base::out)、書き込みポインタ std::basic_streambuf::pptr を書き込み領域内で以下に記述されるように再配置します。
- もし which が std::ios_base::in と std::ios_base::out の両方を含み、バッファが読み書き両用に開かれている場合(すなわち、(which & (std::ios_base::in | std::ios_base::out)) == (std::ios_base::in | std::ios_base::out))、そして dir が std::ios_base::beg または std::ios_base::end のいずれかである場合、読み書き両方のポインタを以下に記述されるように再配置します。
- それ以外の場合、この関数は失敗します。
もし gptr および/または pptr が再配置される場合、それは以下のように行われます。
1) 新しいポインタオフセット newoff(
off_type 型)が決定されます。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 | - | 相対オフセットを適用する基準位置を定義します。次のいずれかの定数を使用できます。
| ||||||||
| which | - | 入力シーケンス、出力シーケンス、またはその両方に影響するかどうかを定義します。これは、次の定数のいずれか、またはそれらの組み合わせです。
|
[編集] 戻り値
成功した場合は 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 | seekoff は newoff + 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) |