Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Moves the read position in a stream.

basic_istream& seekg(
   pos_type _Pos
basic_istream& seekg(
   off_type _Off,
   ios_base::seekdir _Way



An offset to move the read pointer relative to way.


The absolute position in which to move the read pointer.


One of the ios_base::seekdir enumerations.

The stream (*this).

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.

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

int main ( ) 
   using namespace std;
   ifstream file;
   char c, c1; "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;


Header: <istream>

© 2016 Microsoft