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 dir | value of whence |
---|---|
std::basic_ios::beg | SEEK_SET |
std::basic_ios::end | SEEK_END |
std::basic_ios::cur | SEEK_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 |
Constant | Explanation | |
beg | the beginning of a stream | |
end | the ending of a stream | |
cur | the 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 |
Constant | Explanation | |
in | affect the input sequence | |
out | affect 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
二次
另见
pubseekoff | invokes seekoff() (public member function of std::basic_streambuf) |
---|---|
seekpos virtual | repositions the file position, using absolute addressing (virtual protected member function) |
fseek | moves the file position indicator to a specific location in a file (function) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。