std::basic_istream<CharT,Traits>::seekg

Sets input position indicator of the current associated streambuf object.

# Declarations

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

# Parameters

# Return value

*this

# Notes

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().

# 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';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 129C++98there was no way to indicate a failuresets failbit on failure
LWG 136C++98seekg could set the output streamonly sets the input stream
LWG 537C++98the type of off was off_type&corrected to off_type

# See also