名前空間
変種
操作

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

From 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 でなく、かつこのファイルバッファオブジェクトに対して最後に実行された操作が出力であった場合(つまり、出力バッファが空でないか、最後に呼び出された関数が overflow() であった場合)、std::codecvt::unshift を呼び出して必要なアンシフトシーケンスを決定し、overflow() を呼び出してそのシーケンスをファイルに書き込みます。

その後、引数 dirint 型の値 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 - 相対オフセットを適用する基準位置を定義します。次のいずれかの定数を使用できます。
Constant 説明
beg ストリームの先頭
end ストリームの末尾
cur ストリーム位置指示子の現在の位置
which - 入力および/または出力シーケンスのどちらに影響するかを定義します。次の定数のいずれか、またはそれらの組み合わせにすることができます。
Constant 説明
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 <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) [編集]
ファイル位置指示子をファイル内の特定の場所に移動する
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)