istream::seekg
istream&  seekg ( streampos pos );
istream&  seekg ( streamoff off, ios_base::seekdir dir );
istream
  cplusplus.com  

Set position of the get pointer.
  Sets the position of the get pointer. The get pointer determines the next location to be read in the buffer associated to the input stream.  

Parameters.

pos
The new position in the stream buffer. This parameter is an object of type streampos.
off
Integer value of type streamoff representing the offset in the stream's buffer. It is relative to dir parameter.
dir
Seeking direction. It is an object of type ios_base::seekdir that can take any of the following constant values:

Return Value.
  The function returns *this

Example.

// read a file into memory
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  int length;
  char * buffer;

  ifstream is;
  is.open ("test.txt", ios::binary );

  // get length of file:
  is.seekg (0, ios::end);
  length = is.tellg();
  is.seekg (0, ios::beg);

  // allocate memory:
  buffer = new char [length];

  // read data as a block:
  is.read (buffer,length);

  is.close();

  cout.write (buffer,length);

  return 0;
}
  In this example seekg is used to move the get pointer to the end of the stream and then back to the beginning.

Basic template member declaration ( basic_istream<charT,traits> ):
typedef traits::pos_type pos_type;
typedef traits::off_type off_type;
basic_istream<charT,traits> & seekg ( pos_type pos );
basic_istream<charT,traits> & seekg ( off_type off, ios_base::seekdir dir );

See also.
  tellg, ostream::seekp, ostream::tellp
  istream class


© The C++ Resources Network, 2001