| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Public member functions | ||||
(C++11) | ||||
(C++11) | ||||
(C++20) | ||||
| Protected member functions | ||||
basic_stringbuf::seekoff | ||||
| Non-member functions | ||||
(C++11) | ||||
| Exposition-only member functions | ||||
protected: virtual pos_type seekoff( off_type off, | ||
Repositionsstd::basic_streambuf::gptr and/orstd::basic_streambuf::pptr, if possible, to the position that corresponds to exactlyoff characters from beginning, end, or current position of the get and/or put area of the buffer.
Ifgptr and/orpptr is repositioned, it is done as follows:
off_type is determinedContents |
| off | - | relative position to set the next pointer(s) to | ||||||||
| dir | - | defines base position to apply the relative offset to. It can be one of the following constants:
| ||||||||
| which | - | defines whether the input sequences, the output sequence, or both are affected. It can be one or a combination of the following constants:
|
pos_type(newoff) on success,pos_type(off_type(-1)) on failure or ifpos_type cannot represent the resulting stream position.
#include <iostream>#include <sstream> int main(){std::stringstream ss("123");// in/outstd::cout<<"put pos = "<< ss.tellp()<<" get pos = "<< ss.tellg()<<'\n'; // absolute positioning both pointers ss.rdbuf()->pubseekoff(1,std::ios_base::beg);// move both 1 forwardstd::cout<<"put pos = "<< ss.tellp()<<" get pos = "<< ss.tellg()<<'\n'; // try to move both pointers 1 forward from current positionif(-1== ss.rdbuf()->pubseekoff(1,std::ios_base::cur))std::cout<<"moving both pointers from current position failed\n";std::cout<<"put pos = "<< ss.tellp()<<" get pos = "<< ss.tellg()<<'\n'; // move the write pointer 1 forward, but not the read pointer// can also be called as ss.seekp(1, std::ios_base::cur); ss.rdbuf()->pubseekoff(1,std::ios_base::cur,std::ios_base::out);std::cout<<"put pos = "<< ss.tellp()<<" get pos = "<< ss.tellg()<<'\n'; ss<<'a';// write at put positionstd::cout<<"Wrote 'a' at put position, the buffer is now "<< ss.str()<<'\n'; char ch; ss>> ch;std::cout<<"reading at get position gives '"<< ch<<"'\n";}
Output:
put pos = 0 get pos = 0put pos = 1 get pos = 1moving both pointers from current position failedput pos = 1 get pos = 1put pos = 2 get pos = 1Wrote 'a' at put position, the buffer is now 12areading at get position gives '2'
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 55 | C++98 | seekoff returned an undefinedinvalid stream position on failure | pos_type(off_type(-1)) is returned on failure |
| LWG 375 | C++98 | static constant members ofstd::ios_base were misspecified as members ofstd::basic_ios | corrected |
| LWG 432 | C++98 | seekoff might succeed even ifnewoff+ offwould point past the last initialized character | seekoff failsin this case |
| LWG 453 | C++98 | repositioning nullgptr() and/or nullpptr() with a new offset of zero always failed | it can succeed in this case |
| LWG 563 | C++98 | the end pointer could not be used to calculatenewoff because it could not be precisely controlled by the program after resolvingLWG issue 432 | use the high watermark pointer instead |
| invokesseekoff() (public member function of std::basic_streambuf<CharT,Traits>)[edit] | |
[virtual] | repositions the next pointer in the input sequence, output sequence, or both using absolute addressing (virtual protected member function)[edit] |
[virtual] | repositions the file position, using relative addressing (virtual protected member function of std::basic_filebuf<CharT,Traits>)[edit] |
[virtual] | repositions the next pointer in the input sequence, output sequence, or both, using relative addressing (virtual protected member function of std::strstreambuf)[edit] |