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

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

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

  • _Way
    One of the ios_base::seekdir enumerations.

Return Value

The stream (*this).

Remarks

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.

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);   // chars to skip
   file >> c;
   cout << c << endl;

   file.seekg( 0, ios_base::beg );
   file >> c;
   cout << c << endl;

   file.seekg( -1, ios_base::end );
   file >> c1;
   cout << c1 << endl;
}

Input: basic_istream_seekg.txt

0123456789

Output

2
0
9

Requirements

Header: <istream>

Namespace: std

See Also

Concepts

basic_istream Class

basic_istream Members

iostream Programming

iostreams Conventions