public member function
<streambuf> <iostream>

std::basic_streambuf::pubseekoff

pos_type pubseekoff (off_type off, ios_base::seekdir way,
                     ios_base::openmode which = ios_base::in | ios_base::out);
Set internal position pointer to relative position
Calls the protected virtual member seekoff with the same arguments off, way and which.

Member seekoff does nothing in basic_streambuf, but derived classes shall override this behavior to alter the internal pointers appropriately: both basic_filebuf and basic_stringbuf override this virtual member function to set the internal pointer specified by which to a position offset off relative to the direction specified by way.

Parameters

off
Offset value, relative to the way parameter.
Member type off_type is determined by the character traits: generally, it is an alias of the signed integral type streamoff.
way
Object of type ios_base::seekdir. It may take any of the following 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
which
Generally used to determine the position on which of the controlled sequences shall be modified: the input sequence, the output sequence, 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::inModify current position in controlled input sequence
ios_base::outModify current position in controlled output sequence

Return Value

The new position value of the modified position pointer.
The default definition in basic_streambuf always returns -1.
Member type pos_type is determined by the character traits: generally, it is an fpos type (such as streampos) that can be converted to/from integral types.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// get file size using pubseekoff
#include <iostream>     // std::cout, std::streambuf
#include <fstream>      // std::fstream

int main () {
  std::fstream filestr ("test.txt");
  if (filestr) {
    std::streambuf* pbuf = filestr.rdbuf();
    long size = pbuf->pubseekoff(0,filestr.end);
    std::cout << "The file size is " << size << " characters.\n";
    filestr.close();
  }

  return 0;
}


This program prints the size of file test.txt using the value returned by pubseekoff when it repositions the position pointer to the end of the file buffer.

Data races

Modifies the stream buffer object.
Concurrent access to the same stream buffer object may introduce data races.

Exception safety

Basic guarantee: if an exception is thrown, the stream buffer is in a valid state (this also applies to standard derived classes).

Invalid arguments cause undefined behavior.

See also