public member function

std::streambuf::pubseekpos

<streambuf>
streampos pubseekpos ( streampos pos, ios_base::openmode which = ios_base::in | ios_base::out );
Set internal position pointer to absolute position
Calls protected virtual member seekpos, which sets a new position value for one or both of the internal position pointers.

The parameter which determines which of the position pointers is affected: either the get pointer gptr or the put pointer pptr, or both.

Parameters

pos
New absolute position for the position pointer.
This is an object of class streampos (or traits::pos_type for other traits), which can be constructed directly from integral values representing a relative position from the beginning of the stream.
which
Determines which of the internal position pointers shall be modified: the input pointer, the output pointer, or both. It is an object of type ios_base::openmode that for this function may take any combination of the following significant constant values:
valueposition pointer affected
ios_base::ininput position pointer
ios_base::outoutput position pointer

Return Value

The new position value of the modified position pointer.

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
// changing position with pubseekpos
#include <iostream>
#include <fstream>
using namespace std;

int main () {

  int n;
  filebuf* pbuf;
  char buffer[11];

  fstream filestr ("test.txt");
  pbuf = filestr.rdbuf();

  // change position to the 10th character
  pbuf->pubseekpos(10);
  // read 10 characters
  pbuf->sgetn (buffer,10);
  // append null character to string
  buffer[10]=0;

  filestr.close();

  cout << buffer << endl;

  return 0;
}


This example reads and prints 10 characters of a file starting at position 10 (characters 11th to 20th).

Basic template member declaration

( basic_streambuf<charT,traits> )
1
2
typedef traits::pos_type pos_type;
pos_type pubseekpos (pos_type sp, ios_base which = ios_base::in | ios_base::out );


See also