public member function
<streambuf> <iostream>
pos_type pubseekpos (pos_type pos, ios_base::openmode which = ios_base::in | ios_base::out);
Set internal position pointer to absolute position
Calls the protected virtual member seekpos with the same arguments pos and which.
Member seekpos 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 the absolute position pos.
Parameters
- pos
- New absolute position for the position pointer.
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.
- 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:
value | position pointer affected |
ios_base::in | Modify current position in controlled input sequence |
ios_base::out | Modify 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 that can be converted to/from integral types (such as streampos).
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
|
// changing position with pubseekpos
#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); // get size
if (size>20) {
char buffer[11];
// change position to the 10th character
pbuf->pubseekpos(10);
// read 10 characters
pbuf->sgetn (buffer,10);
// append null character to string
buffer[10]=0;
std::cout << buffer << '\n';
}
filestr.close();
}
return 0;
}
|
This example reads and prints 10 characters of a file starting at position 10 (characters 11th to 20th).
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.