std::basic_stringbuf<CharT,Traits,Allocator>::seekoff
提供: cppreference.com
<tbody>
</tbody>
protected: virtual pos_type seekoff(off_type off, ios_base::seekdir dir, ios_base::openmode which = ios_base::in | ios_base::out); |
||
可能であれば、 std::basic_streambuf::gptr または std::basic_streambuf::pptr またはその両方の位置をバッファの get 領域または put 領域またはその両方の先頭、終端または現在位置からちょうど文字 off 個に対応する位置に再設定します。
whichがios_base::inを含み、このバッファが読み込み用に開かれている場合 (つまり(which & ios_base::in) == ios_base::inの場合)、下で説明するように get 領域の内部の読み込みポインタ std::basic_streambuf::gptr の位置を再設定します。whichがios_base::outを含み、このバッファが書き込み用に開かれている場合 (つまり(which & ios_base::out) == ios_base::outの場合)、下で説明するように put 領域の内部の書き込みポインタ std::basic_streambuf::pptr の位置を再設定します。whichがios_base::inとios_base::outをどちらも含み、バッファが読み書き両用に開かれており (つまり(which & (ios_base::in|ios_base::out)) == (ios_base::in|ios_base::out)であり)、dirがios_base::begまたはios_base::endのいずれかの場合、下で説明するように読み書き両方のポインタの位置を再設定します。
ポインタ (gptr または pptr またはその両方) の位置が再設定される場合、それは以下のように行われます。
1) 再設定されるポインタがヌルポインタであり、新しいオフセット
newoff が非ゼロの場合、この関数は失敗します。2)
off_type 型の新しいポインタオフセット newoff が決定されます。a)
dir == ios_base::beg の場合、 newoff はゼロです。b)
dir == ios_base::cur の場合、 newoff はポインタの現在位置 (gptr()-eback() または pptr()-pbase()) です。c)
dir == ios_base::end の場合、 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))。
例
Run this code
#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'
関連項目
[仮想] |
入力シーケンス、出力シーケンス、またはその両方の次ポインタの位置を絶対位置を使用して再設定します ( std::basic_streambuf<CharT,Traits>の仮想プロテクテッドメンバ関数)
|
[仮想] |
入力シーケンス、出力シーケンス、またはその両方の次ポインタの位置を相対位置を使用して再設定します ( std::basic_streambuf<CharT,Traits>の仮想プロテクテッドメンバ関数)
|
[仮想] |
相対位置を使用してファイル位置を再設定します ( std::basic_filebuf<CharT,Traits>の仮想プロテクテッドメンバ関数)
|