std::basic_istream<CharT,Traits>::seekg
来自cppreference.com
                    
                                        
                    < cpp | io | basic istream
                    
                                                            
                    |   basic_istream& seekg( pos_type pos );  | 
||
|   basic_istream& seekg( off_type off, std::ios_base::seekdir dir);  | 
||
设置当前关联 streambuf 对象的输入位置指示器,失败的情况下调用 setstate(std::ios_base::failbit) 。
| 
 进行任何其他动作前,   | 
(C++11 起) | 
seekg 表现为无格式输入函数 (UnformattedInputFunction) ,除了不影响 gcount() 。构造并检查 sentry 对象后,
参数
| pos | - | 设置输入位置指示器到的绝对位置。 | ||||||||||
| off | - | 设置输入位置指示器到的相对位置。 | ||||||||||
| dir | - |  定义应用相对偏移到的基位置。它能为下列常量之一:
  | ||||||||||
返回值
*this
异常
若内部操作抛出异常,则捕获它并设置 badbit 。若对 badbit 设置了 exceptions() ,则重抛该异常。
示例
运行此代码
#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); // 回溯 in >> word2; std::cout << "word1 = " << word1 << '\n' << "word2 = " << word2 << '\n'; }
输出:
word1 = Hello, word2 = Hello,
参阅
|   返回输入位置指示器  (公开成员函数)  | |
|   返回输出位置指示器  ( std::basic_ostream<CharT,Traits> 的公开成员函数)  | |
|   设置输出位置指示器  ( std::basic_ostream<CharT,Traits> 的公开成员函数)  |