std::basic_filebuf<CharT,Traits>::seekpos
From cppreference.com
< cpp | io | basic filebuf
| protected: virtual pos_type seekpos( pos_type sp, |
||
関連付けられたファイルが開かれていない場合 (is_open() == false)、直ちに失敗します。それ以外の場合は、可能であれば、sp が示す位置にファイルポインタを移動させます。
再配置は次のように実行されます。
1) ファイルが書き込み用に開かれている場合、現在のロケールによって必要とされる、出力バッファおよびシフトシーケンスを、overflow() を使用して書き込みます。
2) std::fsetpos() を呼び出すかのように、ファイルポインタを移動させます。
3) ファイルが読み込み用に開かれている場合、必要に応じて入力バッファを更新します。
sp が、同じファイルに対して以前に呼び出された seekoff() または seekpos() によって取得されたものではない場合、動作は未定義です。
目次 |
[編集] パラメータ
| sp | - | 同じファイルに対して以前に呼び出された seekoff() または seekpos() によって取得されたファイル位置 | ||||||
| which | - | 入力および/または出力シーケンスのどちらに影響するかを定義します。次の定数のいずれか、またはそれらの組み合わせにすることができます。
|
[編集] 戻り値
成功した場合は sp、失敗した場合は pos_type(off_type(-1))。
[編集] 注釈
seekpos() は std::basic_streambuf::pubseekpos() によって呼び出されます。これは、std::basic_istream::seekg() および std::basic_ostream::seekp() の単一引数バージョンによって呼び出されます。
多くの実装では、seekpos() で入力バッファは更新されず、次の sgetc() で呼び出される underflow() に委譲されます。
[編集] 例
一部の実装では、seekpos() によって入力バッファがクリアされるため、変更を観測するには2回目の underflow() が必要になります。
このコードを実行
#include <fstream> #include <iostream> struct mybuf : std::filebuf { pos_type seekpos(pos_type sp, std::ios_base::openmode which) { std::cout << "Before seekpos(" << sp << "), size of the get area is " << egptr() - eback() << " with " << egptr() - gptr() << " read positions available.\n"; pos_type rc = std::filebuf::seekpos(sp, which); std::cout << "seekpos() returns " << rc << ".\nAfter the call, " << "size of the get area is " << egptr() - eback() << " with " << egptr() - gptr() << " read positions available.\n"; // uncomment if get area is emptied by seekpos() // std::filebuf::underflow(); // std::cout << "after forced underflow(), size of the get area is " // << egptr() - eback() << " with " // << egptr() - gptr() << " read positions available.\n"; return rc; } }; int main() { mybuf buf; buf.open("test.txt", std::ios_base::in); std::istream stream(&buf); stream.get(); // read one char to force underflow() stream.seekg(2); }
実行結果の例
Before seekpos(2), size of the get area is 110 with 109 read positions available. seekpos() returns 2. After the call, size of the get area is 110 with 108 read positions available.
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 55 | C++98 | seekpos は未定義の値を返しました失敗時に無効なストリーム位置 |
pos_type(off_type(-1)) が失敗時に返される |
| LWG 171 | C++98 | 移動操作のシーケンスが不明瞭でした | 明確化された |
[編集] 関連項目
| seekpos() を呼び出す ( std::basic_streambuf<CharT,Traits> の public メンバ関数) | |
| [virtual] |
相対アドレスを使用してファイル位置を再配置します (仮想 protected メンバ関数) |
| ファイル位置指示子をファイル内の特定の場所に移動する (関数) |