std::basic_stringbuf<CharT,Traits,Allocator>:: seekoff
|
protected
:
virtual
pos_type seekoff
(
off_type off,
|
||
std::basic_streambuf::gptr および/または std::basic_streambuf::pptr を、可能であればバッファのget領域および/またはput領域の先頭、末尾、または現在位置から正確に off 文字分の位置に再配置します。
- 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 が再配置される場合、以下のように行われます:
off_type
として決定されます
目次 |
パラメータ
| off | - | 次のポインタの位置を設定する相対位置 | ||||||||
| dir | - |
相対オフセットを適用する基準位置を定義します。以下の定数のいずれかを指定できます:
|
||||||||
| which | - |
入力シーケンス、出力シーケンス、またはその両方が影響を受けるかどうかを定義します。以下の定数のいずれか、または組み合わせを指定できます:
|
戻り値
pos_type
(
newoff
)
成功時、
pos_type
(
off_type
(
-
1
)
)
失敗時、または
pos_type
が結果のストリーム位置を表現できない場合。
例
#include <iostream> #include <sstream> int main() { std::stringstream ss("123"); // 入出力 std::cout << "put pos = " << ss.tellp() << " get pos = " << ss.tellg() << '\n'; // 両方のポインタを絶対位置指定 ss.rdbuf()->pubseekoff(1, std::ios_base::beg); // 両方を1つ前に移動 std::cout << "put pos = " << ss.tellp() << " get pos = " << ss.tellg() << '\n'; // 現在位置から両方のポインタを1つ前に移動しようとする if (-1 == ss.rdbuf()->pubseekoff(1, std::ios_base::cur)) std::cout << "現在位置からの両方のポインタの移動に失敗しました\n"; std::cout << "put pos = " << ss.tellp() << " get pos = " << ss.tellg() << '\n'; // 書き込みポインタを1つ前に移動(読み取りポインタは移動しない) // 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'; // put位置に書き込み std::cout << "put位置に'a'を書き込み、バッファは現在 " << ss.str() << '\n'; char ch; ss >> ch; std::cout << "get位置での読み取り結果は '" << 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 |
nullの
gptr
(
)
および/または nullの
pptr
(
)
を
オフセットゼロで再配置すると常に失敗した |
この場合は成功することがある |
| LWG 563 | C++98 |
終端ポインタは
LWG issue 432
を解決した後、
プログラムで正確に制御できなかったため newoff の計算に使用できなかった |
代わりにハイ・ウォーターマーク
ポインタを使用する |
関連項目
|
seekoff
を呼び出す
(
std::basic_streambuf<CharT,Traits>
のpublicメンバ関数)
|
|
|
[virtual]
|
絶対アドレス指定を使用して、入力シーケンス、出力シーケンス、または両方の次のポインタを再配置する
(仮想protectedメンバ関数) |
|
[virtual]
|
相対アドレス指定を使用してファイル位置を再配置する
(
std::basic_filebuf<CharT,Traits>
の仮想protectedメンバ関数)
|
|
[virtual]
|
相対アドレス指定を使用して、入力シーケンス、出力シーケンス、または両方の次のポインタを再配置する
(
std::strstreambuf
の仮想protectedメンバ関数)
|