From cppreference.com
protected:virtualpos_typeseekoff(off_typeoff,std::ios_base::seekdirdir,std::ios_base::openmodewhich=std::ios_base::in|std::ios_base::out);Repositions
and/or
, if possible, to the position that corresponds to exactly off characters from beginning, end, or current position of the get and/or put area of the buffer.
If which includes
and this buffer is open for reading (that is, if (which&std::ios_base::in)==std::ios_base::in), then repositions the read pointer
inside the get area as described below
If which includes
and this buffer is open for writing (that is, (which&std::ios_base::out)==std::ios_base::out), then repositions the write pointer
inside the put area as described below
If which includes both
and
and the buffer is open for both reading and writing (that is, (which&(std::ios_base::in|std::ios_base::out))==(std::ios_base::in|std::ios_base::out)), and dir is either
or
, then repositions both read and write pointers as described below.
Otherwise, this function fails.
If
and/or
is repositioned, it is done as follows:
1) The new pointer offset newoff of type off_type is determined
a) if dir==std::ios_base::beg, then newoff is zero
b) if dir==std::ios_base::cur, then newoff is the current position of the pointer (gptr()-eback() or pptr()-pbase())
c) if dir==std::ios_base::end, then newoff is the length of the entire initialized part of the buffer (if
is used, the high watermark pointer minus the beginning pointer)
2) If the pointer to be repositioned is a null pointer and newoff would be non-zero, this function fails.
3) If newoff+off<0 (the repositioning would move the pointer to before the beginning of the buffer) or if newoff+off would point past the end of the buffer (or past the last initialized character in the buffer if
is used), the function fails.
4) Otherwise, the pointer is assigned as if by gptr()=eback()+newoff+off or pptr()=pbase()+newoff+off.
Parameters
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: Constant Explanation
the beginning of a stream
the ending of a stream
the current position of stream position indicator 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
pos_type(newoff) on success, pos_type(off_type(-1)) on failure or if pos_type cannot represent the resulting stream position.
Example
Run this code
#include<iostream>#include<sstream>intmain(){std::stringstreamss("123");// in/outstd::cout<<"put pos = "<<ss.tellp()<<" get pos = "<<ss.tellg()<<'\n';// absolute positioning both pointersss.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';charch;ss>>ch;std::cout<<"reading at get position gives '"<<ch<<"'\n";}Output:
put pos = 0 get pos = 0 put pos = 1 get pos = 1 moving both pointers from current position failed put pos = 1 get pos = 1 put pos = 2 get pos = 1 Wrote 'a' at put position, the buffer is now 12a reading at get position gives '2' 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 seekoff returned an undefined
invalid stream position on failure pos_type(off_type(-1))
is returned on failure
C++98 static constant members of
were
misspecified as members of
corrected
C++98 seekoff might succeed even if newoff+off
would point past the last initialized character seekoff fails
in this case
C++98 repositioning null gptr() and/or null pptr()
with a new offset of zero always failed it can succeed in this case
C++98 the end pointer could not be used to calculate newoff because it could
not be precisely controlled by the program after resolving
use the high watermark
pointer instead See also
invokes seekoff()
(public 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)
[virtual]
repositions the file position, using relative addressing
(virtual protected member function of std::basic_filebuf<CharT,Traits>)
[virtual]
repositions the next pointer in the input sequence, output sequence, or both, using relative addressing
(virtual protected member function of std::strstreambuf)