名前空間
変種
操作

std::basic_filebuf<CharT,Traits>::seekoff

提供: cppreference.com
< cpp‎ | io‎ | basic filebuf
 
 
 
 
protected:

virtual pos_type seekoff( off_type off,
                          std::ios_base::seekdir dir,

                          std::ios_base::openmode which =std::ios_base::in|std::ios_base::out);

可能であれば、ファイルポインタの位置を先頭、終端、またはファイルの現在位置 (dir の値によります) からちょうど文字 off 個に対応する位置に設定します。

紐付けられているファイルが開いていない (is_open()==false) 場合は、直ちに失敗します。

マルチバイト文字エンコーディングが状態依存 (codecvt::encoding()-1 を返した) または可変長 (codecvt::encoding()0 を返した) であり、オフセット off0 でない場合は、直ちに失敗します。 この関数は文字 off 個に対応するバイト数を決定することができません。

dirstd::basic_ios::cur でないかオフセット off0 でなく、この filebuf オブジェクトに対して最も最近行われた操作が出力であった (つまり、 put バッファが空でないか、最も最近呼ばれた関数が overflow() であった) 場合は、必要なシフト解除シーケンスを決定するために std::codecvt::unshift を呼び、そのシーケンスを overflow() を呼ぶことによってファイルに書き込みます。

その後、引数 dirint 型の値 whence に以下のように変換します。

dir の値 whence の値
std::basic_ios::begSEEK_SET
std::basic_ios::endSEEK_END
std::basic_ios::curSEEK_CUR

その後、文字エンコーディングが固定幅 (codecvt::encoding() が何らかの正の値 width を返す) の場合は、 std::fseek(file, width*off, whence) によって行われたかのようにファイルポインタを移動させます。

そうでなければ、 std::fseek(file, 0, whence) によって行われたかのようにファイルポインタを移動させます。

std::basic_filebuf はファイル位置を1個しか維持管理しないため、基底クラスの関数シグネチャによって要求される openmode 引数は、通常、無視されます。

目次

[編集]引数

off - 位置指示子を設定する相対位置
dir - 相対オフセットを適用するベースの位置を定義します。 以下の定数のいずれかを指定できます。
定数 説明
beg ストリームの先頭
end ストリームの終端
cur ストリームの位置指示子の現在位置
which - 入力と出力のどちらに影響を与えるかを定義します。 以下の定数のいずれかまたは組み合わせを指定できます。
定数 説明
in 入力シーケンスに影響を与えます
out 出力シーケンスに影響を与えます

[編集]戻り値

結果のファイル位置を格納する pos_type 型の新たに構築されたオブジェクト、または失敗した場合は pos_type(off_type(-1))

[編集]ノート

seekoff()std::basic_streambuf::pubseekoff によって呼ばれ、それは std::basic_istream::seekgstd::basic_ostream::seekpstd::basic_istream::tellg および std::basic_ostream::tellp によって呼ばれます。

[編集]

#include <iostream>#include <fstream>#include <locale>int main(){// prepare a 10-byte file holding 4 characters in UTF8std::ofstream("text.txt")<< u8"z\u00df\u6c34\U0001d10b";// or u8"zß水𝄋"// or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";   // open using a non-converting encodingstd::ifstream f1("text.txt");std::cout<<"f1's locale's encoding() returns "<<std::use_facet<std::codecvt<char, char, std::mbstate_t>>(f1.getloc()).encoding()<<'\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-8std::wifstream f2("text.txt"); f2.imbue(std::locale("en_US.UTF-8"));std::cout<<"f2's locale's encoding() returns "<<std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(f2.getloc()).encoding()<<'\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

[編集]関連項目

seekoff() を呼びます
(std::basic_streambuf<CharT,Traits>のパブリックメンバ関数)[edit]
[仮想]
絶対位置を使用してファイル位置を再設定します
(仮想プロテクテッドメンバ関数)[edit]
ファイル位置指示子をファイル内の指定された位置に移動させます
(関数)[edit]
close