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

std::basic_istream::seekg

性病:基本[医]IStream::Seekg

basic_istream& seekg( pos_type pos
basic_istream& seekg( off_type off, std::ios_base::seekdir dir

设置当前关联的输入位置指示符。streambuf对象。如果失败,呼叫setstate(std::ios_base::failbit)...

Before doing anything else, seekg clears eofbit.(since C++11)

seekg表现为UnformattedInputFunction,除了gcount()不受影响。在构造和检查哨兵对象之后,

1%29将输入位置指示符设置为相对于文件%29值开头的绝对%28。pos.具体而言,执行rdbuf()->pubseekpos(pos,std::ios_base::in)...

2%29将输入位置指示器设置为位置。off相对于位置,由dir.具体而言,执行rdbuf()->pubseekoff(off, dir,std::ios_base::in)...

参数

pos-absolute position to set the input position indicator to.
off-relative position to set the input 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

返回值

*this...

例外

failure如果发生错误%28,则错误状态标志不是goodbit29%和exceptions()将被抛向那个州。

如果内部操作抛出异常,则会捕获该操作,并且badbit已经设定好了。如果exceptions()设置为badbit,异常将被重新抛出。

二次

#include <iostream> #include <string> #include <sstream> int main() { std::string str = "Hello, world"; std::istringstream in(str std::string word1, word2; in >> word1; in.seekg(0 // rewind in >> word2; std::cout << "word1 = " << word1 << '\n' << "word2 = " << word2 << '\n'; }

二次

产出:

二次

word1 = Hello, word2 = Hello,

二次

另见

tellgreturns the input position indicator (public member function)
tellpreturns the output position indicator (public member function of std::basic_ostream)
seekpsets the output position indicator (public member function of std::basic_ostream)

© cppreference.com

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

http://en.cppreference.com/w/cpp/io/basic[医]IStream/Sekg