Share via


basic_istream::seekg

Moves the read position in a stream.

basic_istream<Elem, Tr>& seekg(
    pos_type pos
);
basic_istream<Elem, Tr>& seekg(
    off_type off,
    ios_base::seekdir way
);

Parameters

  • pos
    The absolute position in which to move the read pointer.

  • off
    An offset to move the read pointer relative to way.

  • way
    One of the ios_base::seekdir enumerations.

Return Value

The stream (*this).

Remarks

The first member function performs an absolute seek, the second member function performs a relative seek.

Note

Do not use the second member function with text files, because Standard C++ does not support relative seeks in text files.

If fail is false, the first member function calls newpos = rdbuf -> pubseekpos(pos), for some pos_type temporary object newpos. If fail is false, the second function calls newpos = rdbuf -> pubseekoff(off, way). In either case, if (off_type)newpos == (off_type)(-1) (the positioning operation fails), the function calls istr.setstate(failbit). Both functions return *this.

If fail is true, the member functions do nothing.

Example

// basic_istream_seekg.cpp
// compile with: /EHsc
#include <iostream>
#include <fstream>

int main ( ) 
{
   using namespace std;
   ifstream file;
   char c, c1;

   file.open( "basic_istream_seekg.txt" );
   file.seekg(2);   // seek to position 2
   file >> c;
   cout << c << endl;
}

Input: basic_istream_seekg.txt

0123456789

Output

2

Requirements

Header: <istream>

Namespace: std

See Also

Reference

basic_istream Class

iostream Programming

iostreams Conventions