basic_istream& seekg( pos_type pos );
(1) (2)Sets input position indicator of the current associated streambuf
object.
seekg
clears eofbit
. (since C++11)
seekg
behaves as UnformattedInputFunction, except that gcount() is not affected. After constructing and checking the sentry object,
if
fail() != true, sets the input position indicator to position
off, relative to position, defined by
dir. Specifically, executes
rdbuf()->pubseekoff(off, dir, std::ios_base::in). In case of failure, calls
setstate(std::ios_base::failbit).
[edit] Parameters pos - absolute position to set the input position indicator to off - relative position (positive or negative) 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 [edit] Return value*this
[edit] Exceptionsfailureif 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.
seekg(n) is not necessarily equivalent to seekg(n, ios::beg). std::basic_ifstream, for example, requires the absolute position n to come from tellg().
[edit] Example#include <iostream> #include <sstream> #include <string> 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,[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 129 C++98 there was no way to indicate a failure setsfailbit
on failure LWG 136 C++98 seekg
could set the output stream only sets the input stream LWG 537 C++98 the type of off was off_type&
corrected to off_type
[edit] See also returns the input position indicator
std::basic_ostream<CharT,Traits>
) [edit] sets the output position indicator
std::basic_ostream<CharT,Traits>
) [edit] invokes seekpos()
std::basic_streambuf<CharT,Traits>
) [edit] repositions the file position, using absolute addressing
std::basic_filebuf<CharT,Traits>
) [edit] repositions the next pointer in the input sequence, output sequence, or both using absolute addressing
std::basic_stringbuf<CharT,Traits,Allocator>
) [edit] repositions the next pointer in the input sequence, output sequence, or both using absolute addressing
std::strstreambuf
) [edit] invokes seekoff()
std::basic_streambuf<CharT,Traits>
) [edit] repositions the file position, using relative addressing
std::basic_filebuf<CharT,Traits>
) [edit] repositions the next pointer in the input sequence, output sequence, or both, using relative addressing
std::basic_stringbuf<CharT,Traits,Allocator>
) [edit] repositions the next pointer in the input sequence, output sequence, or both, using relative addressing
std::strstreambuf
) [edit]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4