名稱空間
變體
操作

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),並且偏移量 off 不為 0,則立即失敗:此函式無法確定與 off 個字元對應的位元組數。

如果 dir 不是 std::basic_ios::cur,或者偏移量 off 不為 0,並且在此 filebuf 物件上執行的最近操作是輸出(即,put 緩衝區不為空,或者最近呼叫的函式是 overflow()),則呼叫 std::codecvt::unshift 以確定所需的 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 - - 定義影響輸入及/或輸出序列的哪個。它可以是下列常量之一或組合
常量 解釋
in 影響輸入序列
out 影響輸出序列

[編輯] 返回值

一個新構造的 pos_type 型別物件,儲存最終的檔案位置;失敗時返回 pos_type(off_type(-1))

[編輯] 注意

seekoff()std::basic_streambuf::pubseekoff 呼叫,而後者由 std::basic_istream::seekgstd::basic_ostream::seekpstd::basic_istream::tellgstd::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++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 55 C++98 seekoff 失敗時返回未定義
的非法流位置
pos_type(off_type(-1))
在失敗時返回

[編輯] 另請參閱

呼叫 seekoff()
(std::basic_streambuf<CharT,Traits> 的公共成員函式) [編輯]
[虛擬函式]
使用絕對地址重新定位檔案位置
(虛擬保護成員函式) [編輯]
將檔案位置指示器移動到檔案中特定位置
(函式) [編輯]