std::strstreambuf::seekoff
std::strStreambuf::Sekoff
protected: virtual pos_type seekoff(off_type off, ios_base::seekdir way, ios_base::openmode which = ios_base::in | ios_base::out | | |
---|
再定位std::basic_streambuf::gptr
和/或std::basic_streambuf::pptr
,如果可能的话,它的位置正好对应于off
缓冲区的GET和/或PUT区域的起始、结束或当前位置中的字符。
- 如果
which
包括ios_base::in
这个缓冲区是打开的,然后重新定位读取指针。std::basic_streambuf::gptr
在GET区域内,如下所述
- 如果
which
包括ios_base::out
这个缓冲区是打开的,然后重新定位写指针。std::basic_streambuf::pptr
在PUT区域内,如下所述
- 如果
which
包括两者ios_base::in
和ios_base::out
并且缓冲区是开放的,既可以读也可以写,而且when
是ios_base::beg
或ios_base::end
,然后重新定位读和写指针,如下所述。
- 否则,此函数将失败。
如果指针%28gptr
或pptr
或者两种%29都被重新定位,其操作如下:
1%29如果要重新定位的指针是空指针和新偏移量newoff
如果是非零,则此函数将失败。
2%29新指针偏移量newoff
类型off_type
已确定
A%29如果way == ios_base::beg
,然后newoff
是零
B%29way == ios_base::cur
,然后newoff
指针%28的当前位置。gptr()-eback()
或pptr()-pbase()
%29
C%29如果way == ios_base::end
,然后newoff
是缓冲区%28的整个初始化部分的长度(如果使用过分配,则高水印指针减去起始指针%29)。
3%29newoff + off
为负值或超出缓冲区初始化部分的界限,则函数将失败。
4%29否则,指针将被分配为gptr() = eback() + newoff + off
或pptr() = pbase() + newoff + off
参数
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 beg the beginning of a stream end the ending of a stream cur the current position of stream position indicator |
Constant | Explanation | |
beg | the beginning of a stream | |
end | the ending of a stream | |
cur | 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 in affect the input sequence out affect the output sequence |
Constant | Explanation | |
in | affect the input sequence | |
out | affect the output sequence |
返回值
pos_type(newoff)
在成功的时候,pos_type(off_type(-1))
论失败与失败[医]类型不能表示结果流位置。
例
二次
#include <iostream>
#include <strstream>
int main()
{
char a[] = "123";
std::strstream ss(a, sizeof a // in/out
std::cout << "put pos = " << ss.tellp()
<< " get pos = " << ss.tellg() << '\n';
// absolute positioning both pointers
ss.rdbuf()->pubseekoff(1, std::ios_base::beg // move both forward
std::cout << "put pos = " << ss.tellp()
<< " get pos = " << ss.tellg() << '\n';
// try to move both pointers 1 forward from current position
if(-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 position
std::cout << "Wrote 'a' at put position, the buffer is now: '";
std::cout.write(a, sizeof a
std::cout << "'\n";
char ch;
ss >> ch;
std::cout << "reading at get position gives '" << ch << "'\n";
}
二次
产出:
二次
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'
二次
另见
seekpos virtual | repositions the next pointer in the input sequence, output sequence, or both using absolute addressing (virtual protected member function of std::basic_streambuf) |
---|---|
seekoff virtual | repositions the next pointer in the input sequence, output sequence, or both, using relative addressing (virtual protected member function of std::basic_streambuf) |
seekoff virtual | repositions the next pointer in the input sequence, output sequence, or both, using relative addressing (virtual protected member function of std::basic_stringbuf) |
seekoff virtual | repositions the file position, using relative addressing (virtual protected member function of std::basic_filebuf) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。