From cppreference.com
protected:virtualpos_typeseekpos(pos_typesp,std::ios_base::openmodewhich=std::ios_base::in|std::ios_base::out);(deprecated in C++98)
(removed in C++26)Repositions
and/or
, if possible, to the position indicated by sp.
If
is set in which, attempts to reposition gptr() (the next pointer in the get area). If
is set in which, attempts to reposition pptr() (the next pointer in the put area). If neither bit is set in which, the operation fails.
Each next pointer is repositioned as follows:
If the next pointer is null, the operation fails.
Otherwise, the new offset newoff (of type off_type) is determined by calling sp.offset(). If newoff is negative, out of bounds of the buffer, or invalid, the operation fails.
Otherwise, the next pointer is assigned as if by gptr()=eback()+newoff or pptr()=pbase()+newoff.
Parameters
sp - stream position, such as one obtained by
or seekpos()which - defines whether the input sequences, the output sequence, or both are affected. It can be one or a combination of the following constants: Constant Explanation
affect the input sequence
affect the output sequence Return value
The resultant offset converted to pos_type on success or pos_type(off_type(-1)) on failure.
Notes
seekpos() is called by
std::basic_streambuf::pubseekpos()
, which is called by the single-argument versions of
and
.
Example
Run this code
#include<cstring>#include<iostream>#include<strstream>structmybuf:std::strstreambuf{mybuf(constchar*str):std::strstreambuf(str,std::strlen(str)){}pos_typeseekpos(pos_typesp,std::ios_base::openmodewhich){std::cout<<"Before seekpos("<<sp<<"), size of the get area is "<<egptr()-eback()<<" with "<<egptr()-gptr()<<" read positions available.\n";pos_typerc=std::strstreambuf::seekpos(sp,which);std::cout<<"seekpos() returns "<<rc<<".\nAfter the call, "<<"size of the get area is "<<egptr()-eback()<<" with "<<egptr()-gptr()<<" read positions available.\n";returnrc;}};intmain(){mybufbuf("12345");std::iostreamstream(&buf);stream.seekg(2);}Output:
Before seekpos(2), size of the get area is 5 with 5 read positions available. seekpos() returns 2. After the call, size of the get area is 5 with 3 read positions available. Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 seekpos returned an undefined
invalid stream position on failure pos_type(off_type(-1))
is returned on failure See also
[virtual]
repositions the next pointer in the input sequence, output sequence, or both, using relative addressing
(virtual protected member function)
[virtual]
repositions the next pointer in the input sequence, output sequence, or both using absolute addressing
(virtual protected member function of std::basic_streambuf<CharT,Traits>)
[virtual]
repositions the next pointer in the input sequence, output sequence, or both using absolute addressing
(virtual protected member function of std::basic_stringbuf<CharT,Traits,Allocator>)
[virtual]
repositions the file position, using absolute addressing
(virtual protected member function of std::basic_filebuf<CharT,Traits>)