public member function
<streambuf> <iostream>
streampos pubseekpos (streampos 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 streambuf, but derived classes shall override this behavior to alter the internal pointers appropriately: both filebuf and 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.
streampos is a positioning type that can be converted to/from integral types (an fpos type).
- 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 streambuf always returns -1
.
streampos is a positioning type that can be converted to/from integral types (an fpos type).
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.
See also
- streambuf::pubseekoff
- Set internal position pointer to relative position (public member function)
- streambuf::seekpos
- Set internal position pointer to absolute position (protected virtual member function)
- filebuf::seekpos
- Set position pointer to absolute position (protected virtual member function)
- stringbuf::seekpos
- Set position pointer to absolute position (protected virtual member function)