std::basic_filebuf<CharT,Traits>::seekoff
| protected: virtual pos_type seekoff( off_type off, |
||
ファイルポインタを、可能であれば、ファイルの先頭、末尾、または現在の位置から(dirの値に応じて)off文字分だけ離れた位置に移動させます。
関連付けられたファイルが開かれていない場合(is_open() == false)、直ちに失敗します。
マルチバイト文字エンコーディングが状態依存(codecvt::encoding() が -1 を返した)または可変長(codecvt::encoding() が 0 を返した)であり、オフセット off が 0 でない場合、直ちに失敗します。この関数は、off 文字に対応するバイト数を決定できません。
もし dir が std::basic_ios::cur でなく、またはオフセット off が 0 でなく、かつこのファイルバッファオブジェクトに対して最後に実行された操作が出力であった場合(つまり、出力バッファが空でないか、最後に呼び出された関数が overflow() であった場合)、std::codecvt::unshift を呼び出して必要なアンシフトシーケンスを決定し、overflow() を呼び出してそのシーケンスをファイルに書き込みます。
その後、引数 dir を int 型の値 whence に以下のように変換します。
| dir の値 | whence の値 |
| std::basic_ios::beg | SEEK_SET |
| std::basic_ios::end | SEEK_END |
| std::basic_ios::cur | SEEK_CUR |
次に、文字エンコーディングが固定幅である場合(codecvt::encoding() が正の数 width を返す場合)、ファイルポインタを std::fseek(file, width*off, whence) のように移動させます。
それ以外の場合、ファイルポインタを std::fseek(file, 0, whence) のように移動させます。
基底クラスの関数シグネチャで必要とされる `openmode` 引数は、通常無視されます。なぜなら、std::basic_filebuf は単一のファイル位置のみを管理するからです。
目次 |
[編集] パラメータ
| off | - | 位置インジケータを設定する相対位置 | ||||||||
| dir | - | 相対オフセットを適用する基準位置を定義します。次のいずれかの定数を使用できます。
| ||||||||
| which | - | 入力および/または出力シーケンスのどちらに影響するかを定義します。次の定数のいずれか、またはそれらの組み合わせにすることができます。
|
[編集] 戻り値
結果のファイル位置を格納する `pos_type` 型の新しいオブジェクト。失敗した場合は pos_type(off_type(-1)) です。
[編集] 注記
seekoff() は、std::basic_streambuf::pubseekoff によって呼び出され、これは std::basic_istream::seekg、std::basic_ostream::seekp、std::basic_istream::tellg、および std::basic_ostream::tellp によって呼び出されます。
[編集] 例
#include <fstream> #include <iostream> #include <locale> template<typename CharT> int get_encoding(const std::basic_istream<CharT>& stream) { using Facet = std::codecvt<CharT, char, std::mbstate_t>; return std::use_facet<Facet>(stream.getloc()).encoding(); } int main() { // prepare a 10-byte file holding 4 characters ("zß水𝄋") in UTF-8 std::ofstream("text.txt") << "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b"; // open using a non-converting encoding std::ifstream f1("text.txt"); std::cout << "f1's locale's encoding() returns " << get_encoding(f1) << '\n' << "pubseekoff(3, beg) returns " << f1.rdbuf()->pubseekoff(3, std::ios_base::beg) << '\n' << "pubseekoff(0, end) returns " << f1.rdbuf()->pubseekoff(0, std::ios_base::end) << '\n'; // open using UTF-8 std::wifstream f2("text.txt"); f2.imbue(std::locale("en_US.UTF-8")); std::cout << "f2's locale's encoding() returns " << get_encoding(f2) << '\n' << "pubseekoff(3, beg) returns " << f2.rdbuf()->pubseekoff(3, std::ios_base::beg) << '\n' << "pubseekoff(0, end) returns " << f2.rdbuf()->pubseekoff(0, std::ios_base::end) << '\n'; }
出力
f1's locale's encoding() returns 1 pubseekoff(3, beg) returns 3 pubseekoff(0, end) returns 10 f2's locale's encoding() returns 0 pubseekoff(3, beg) returns -1 pubseekoff(0, end) returns 10
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 55 | C++98 | seekoff は未定義の値を返していた失敗時に無効なストリーム位置 |
pos_type(off_type(-1)) が失敗時に返される |
[編集] 関連項目
| seekoff() を呼び出す ( std::basic_streambuf<CharT,Traits> の public メンバ関数) | |
| [virtual] |
絶対アドレスを使用してファイル位置を再配置します (virtual protected member function) |
| ファイル位置指示子をファイル内の特定の場所に移動する (関数) |