Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
IOstream Library
manipulators
classes:
filebuf
fstream
ifstream
ios
iostream
ios_base
istream
istringstream
ofstream
ostream
ostringstream
streambuf
stringbuf
stringstream
objects:
cerr
cin
clog
cout
types:
fpos
streamoff
streampos
streamsize
istream
istream::istream
istream::~istream
member classes:
istream::sentry
member functions:
istream::gcount
istream::get
istream::getline
istream::ignore
istream::operator>>
istream::peek
istream::putback
istream::read
istream::readsome
istream::seekg
istream::sync
istream::tellg
istream::unget


istream::seekg

public member function
istream& seekg ( streampos pos );
istream& seekg ( streamoff off, ios_base::seekdir dir );

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 source associated to the stream.

Parameters

pos
The new position in the stream buffer. This parameter is an integral value of type streampos.
off
Integral value of type streamoff representing the offset to be applied relative to an absolute position specified in the dir parameter.
dir
Seeking direction. It is an object of type ios_base::seekdir that specifies an absolute position from where the offset parameter off is applied. It can take any of the following member constant values:
valueoffset is relative to...
ios_base::begbeginning of the stream buffer
ios_base::curcurrent position in the stream buffer
ios_base::endend of the stream buffer

Return Value

The function returns *this.

Errors are signaled by modifying the internal state flags:

flagerror
eofbit-
failbitThe parameter(s) describe a position that could not be reached.
badbitAn error other than the above happened.

Additionally, in any of these cases, if the appropriate flag has been set with member function ios::exceptions, an exception of type ios_base::failure is thrown.


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// load 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);
  delete[] buffer;
  return 0;
}


In this example seekg is used to move the get pointer to the end of the file, and then back to the beginning.

Basic template member declaration

( basic_istream<charT,traits> )
1
2
3
4
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