basic_istream& seekg( pos_type pos ); | ||
basic_istream& seekg( off_type off, std::ios_base::seekdir dir); |
Sets input position indicator of the current associated streambuf
object. In case of failure, calls setstate(std::ios_base::failbit)
.
Before doing anything else, seekg clears eofbit . | (since C++11) |
seekg
behaves as UnformattedInputFunction, except that gcount()
is not affected. After constructing and checking the sentry object,
pos
. Specifically, executes rdbuf()->pubseekpos(pos, std::ios_base::in)
.off
, relative to position, defined by dir
. Specifically, executes 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:
|
*this
.
failure
if an error occurred (the error state flag is not goodbit
) and exceptions()
is set to throw for that state. If an internal operation throws an exception, it is caught and badbit
is set. If exceptions()
is set for badbit
, the exception is rethrown.
#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'; }
Output:
word1 = Hello, word2 = Hello,
returns the input position indicator (public member function) |
|
returns the output position indicator (public member function of std::basic_ostream ) |
|
sets the output position indicator (public member function of std::basic_ostream ) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/io/basic_istream/seekg