std::strstreambuf::seekpos
std::strStreambuf::Sekpos
protected: virtual pos_type seekpos(pos_type sp, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out | | |
---|
再定位std::basic_streambuf::gptr
和/或std::basic_streambuf::pptr
,如果可能的话,按照sp
...
如果std::ios_base::in
设为which
,试图重新定位gptr()
%28获取区域%29中的下一个指针。如果std::ios_base::out
设为which
,试图重新定位pptr()
%28是PUT区域%29中的下一个指针。如果没有设置位which
操作失败。
每个下一个指针被重新定位如下:
1%29如果下一个指针为空,则操作失败。
2%29否则,新的冲销newoff
28%类型off_type
%29是通过调用sp.offset()
.如果newoff
为负,超出缓冲区的界限,或无效,操作失败。
3%29否则,下一个指针将被赋值为gptr() = eback()+newoff
或pptr() = pbase()+newoff
...
参数
sp | - | stream position, such as one obtained by seekoff() 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 in affect the input sequence out affect the output sequence |
Constant | Explanation | |
in | affect the input sequence | |
out | affect the output sequence |
返回值
结果偏移量转换为pos_type
关于成功或pos_type(off_type(-1))
在失败的时候。
注记
seekpos()
被std::basic_streambuf::pubseekpos()
的单参数版本调用std::basic_istream::seekg()
和std::basic_ostream::seekp()
...
例
二次
#include <strstream>
#include <cstring>
#include <iostream>
struct mybuf : std::strstreambuf
{
mybuf(const char* str) : std::strstreambuf(str, std::strlen(str)) {}
pos_type seekpos(pos_type sp, std::ios_base::openmode which) {
std::cout << "Before seekpos(" << sp << "), size of the get area is "
<< egptr()-eback() << " with "
<< egptr()-gptr() << " read positions available\n";
pos_type rc = 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";
return rc;
}
};
int main()
{
mybuf buf("12345"
std::iostream stream(&buf
stream.seekg(2
}
二次
产出:
二次
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
二次
另见
pubseekpos | invokes seekpos() (public member function of std::basic_streambuf) |
---|---|
seekpos virtual | repositions the next pointer in the input sequence, output sequence, or both using absolute addressing (virtual protected member function of std::basic_stringbuf) |
seekpos virtual | repositions the file position, using absolute addressing (virtual protected member function of std::basic_filebuf) |
seekoff virtual | repositions the next pointer in the input sequence, output sequence, or both, using relative addressing (virtual protected member function) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。