Namespaces
Variants

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

From cppreference.net
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 を返す場合)であり、オフセット off 0 でない場合、即座に失敗します:この関数は off 文字に対応するバイト数を決定できません。

dir std::basic_ios::cur でない、またはオフセット off 0 でなく、かつこのfilebufオブジェクトに対する直近の操作が出力(つまり、putバッファが空でないか、直近に呼び出された関数が 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 - 相対オフセットを適用する基準位置を定義します。以下の定数のいずれかです:
定数 説明
beg ストリームの先頭
end ストリームの末尾
cur ストリーム位置指示子の現在位置
which - 入力および/または出力シーケンスのどれに影響を与えるかを定義します。以下の定数の1つまたは組み合わせです:
定数 説明
in 入力シーケンスに影響
out 出力シーケンスに影響

戻り値

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()
{
    // UTF-8で4文字("zß水𝄋")を保持する10バイトのファイルを準備
    std::ofstream("text.txt") << "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";
    // 変換を行わないエンコーディングでオープン
    std::ifstream f1("text.txt");
    std::cout << "f1のロケールのencoding()は"
              << get_encoding(f1) << '\n'
              << "pubseekoff(3, beg)は"
              << f1.rdbuf()->pubseekoff(3, std::ios_base::beg) << '\n'
              << "pubseekoff(0, end)は"
              << f1.rdbuf()->pubseekoff(0, std::ios_base::end) << '\n';
    // UTF-8を使用してオープン
    std::wifstream f2("text.txt");
    f2.imbue(std::locale("en_US.UTF-8"));
    std::cout << "f2のロケールのencoding()は"
              << get_encoding(f2) << '\n'
              << "pubseekoff(3, beg)は"
              << f2.rdbuf()->pubseekoff(3, std::ios_base::beg) << '\n'
              << "pubseekoff(0, end)は"
              << f2.rdbuf()->pubseekoff(0, std::ios_base::end) << '\n';
}

出力:

f1のロケールのencoding()は1
pubseekoff(3, beg)は3
pubseekoff(0, end)は10
f2のロケールのencoding()は0
pubseekoff(3, beg)は-1
pubseekoff(0, end)は10

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 55 C++98 seekoff は失敗時に未定義の
無効なストリーム位置を返していた
pos_type ( off_type ( - 1 ) )
が失敗時に返される

関連項目

seekoff を呼び出す seekoff ( )
( std::basic_streambuf<CharT,Traits> のpublicメンバ関数)
[virtual]
絶対アドレス指定を使用してファイル位置を再配置する
(virtual protectedメンバ関数)
ファイル位置インジケータをファイル内の特定の位置に移動する
(関数)