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

std::basic_filebuf::seekpos

性病:基本[医]filebuf::Sekpos

protected: virtual pos_type seekpos( pos_type sp, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out

如果可能,将文件指针重新定位到sp...

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

如果文件是打开的,则首先写入PUT区域和当前注入的区域设置所需的任何未移位序列,使用overflow()...

然后重新定位文件指针,就像通过调用std::fsetpos()...

如果文件是打开的,如果有必要,更新GET区域。

如果sp不是通过调用seekoff()seekpos()在同一文件中,行为未定义。

参数

sp-file position obtained by seekoff() or seekpos() called earlier on the same file
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

返回值

sp关于成功或pos_type(off_type(-1))在失败的时候。

注记

seekpos()std::basic_streambuf::pubseekpos()的单参数版本调用std::basic_istream::seekg()std::basic_ostream::seekp()...

中的许多实现不更新GET区域。seekpos(),委托给underflow()被巢穴称为sgetc()...

在某些实现中,GET区域被Sekpos%28%29清空,而第二次底流%28%29是观察效果所必需的。

二次

#include <fstream> #include <iostream> struct mybuf : std::filebuf { pos_type seekpos(pos_type sp, std::ios_base::openmode which) { std::cout << "Before seekpos(" << sp << "), size of the get area is " << egptr()-eback() << " with " << egptr()-gptr() << " read positions available\n"; pos_type rc = std::filebuf::seekpos(sp, which std::cout << "seekpos() returns " << rc << ".\nAfter the call, " << "size of the get area is " << egptr()-eback() << " with " << egptr()-gptr() << " read positions available\n"; // uncomment if get area is emptied by seekpos() // std::filebuf::underflow( // std::cout << "after forced underflow(), size of the get area is " // << egptr()-eback() << " with " // << egptr()-gptr() << " read positions available\n"; return rc; } }; int main() { mybuf buf; buf.open("test.txt", std::ios_base::in std::istream stream(&buf stream.get( // read one char to force underflow() stream.seekg(2 }

二次

可能的产出:

二次

Before seekpos(2), size of the get area is 110 with 109 read positions available seekpos() returns 2. After the call, size of the get area is 110 with 108 read positions available

二次

另见

pubseekposinvokes seekpos() (public member function of std::basic_streambuf)
seekoff virtualrepositions the file position, using relative 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[医]filebuf/sekpos