在线文档教程
C++
输入/输出 | Input/output

std::basic_filebuf::seekoff

性病:基本[医]FILEBOF::Sekoff

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

如果可能的话,将文件指针重新定位到与之完全对应的位置。off文件%28的起始、结束或当前位置中的字符,取决于dir...

如果关联文件未打开%28is_open()==false立即失败。

如果多字节字符编码是状态相关的%28codecvt::encoding()退回来-1%29或可变长度%28codecvt::encoding()退回来​0​%29及抵销off不是​0​,立即失败:此函数无法确定对应于off人物。

如果dir不是std::basic_ios::cur或者抵消off不是​0​,而对这个filebuf对象执行的最不满的操作是输出%28,即PUT缓冲区不是空的,或者最近调用的函数是overflow()%29,然后呼叫std::codecvt::unshift以确定所需的未移位序列,并通过调用overflow()...

然后转换参数dir到一个值whence类型int详情如下:

value of dirvalue of whence
std::basic_ios::begSEEK_SET
std::basic_ios::endSEEK_END
std::basic_ios::curSEEK_CUR

然后,如果字符编码是固定宽度%28codecvt::encoding()返回一些正数width,将文件指针移动到std::fseek(file, width*off, whence)...

否则,将文件指针移动为std::fseek(file, 0, whence)...

openmode参数,基类函数签名所需的参数通常被忽略,因为std::basic_filebuf只维护一个文件位置。

参数

off-relative position to set the position indicator to.
dir-defines base position to apply the relative offset to. It can be one of the following constants: Constant Explanation beg the beginning of a stream end the ending of a stream cur the current position of stream position indicator
ConstantExplanation
begthe beginning of a stream
endthe ending of a stream
curthe current position of stream position indicator
which-defines which of the input and/or output sequences to affect. It can be one or a combination of the following constants: Constant Explanation in affect the input sequence out affect the output sequence
ConstantExplanation
inaffect the input sequence
outaffect the output sequence

返回值

新构造的类型对象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 <iostream> #include <fstream> #include <locale> int main() { // prepare a 10-byte file holding 4 characters in UTF8 std::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 encoding std::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-8 std::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

二次

另见

pubseekoffinvokes seekoff() (public member function of std::basic_streambuf)
seekpos virtualrepositions the file position, using absolute addressing (virtual protected member function)
fseekmoves the file position indicator to a specific location in a file (function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/io/basic[医]文件/查询