< cpp‎ | io‎ | basic istream
Revision as of 21:11, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dcl ">

<td class="t-dcl-nopad">
basic_istream& seekg( pos_type pos );

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
basic_istream& seekg( off_type off, std::ios_base::seekdir dir);

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Sets input position indicator of the current associated streambuf object. In case of failure, calls setstate(std::ios_base::failbit).

First, clears eofbit(since C++11), then behaves as UnformattedInputFunction, except that gcount() is not affected. After constructing and checking the sentry object,

1) sets the input position indicator to absolute (relative to the beginning of the file) value pos. Specifically, executes rdbuf()->pubseekpos(pos, std::ios_base::in).

2) sets the input position indicator to position 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:
Constant Explanation
beg the beginning of a stream
end the ending of a stream
cur the current position of stream position indicator

Return value



#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,

See also

Template:cpp/io/basic istream/dcl list tellgTemplate:cpp/io/basic ostream/dcl list tellpTemplate:cpp/io/basic ostream/dcl list seekp