Export (0) Print
Expand All

basic_istream::seekg 

Moves the read position in a stream.


basic_istream& seekg(
   pos_type _Pos
);
basic_istream& 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.

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;

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

2
0
9

Header: <istream>

Community Additions

ADD
Show:
© 2014 Microsoft