在线文档教程
C++
输入/输出 | Input/output

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否则,新的冲销newoff28%类型off_type%29是通过调用sp.offset().如果newoff为负,超出缓冲区的界限,或无效,操作失败。

3%29否则,下一个指针将被赋值为gptr() = eback()+newoffpptr() = 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
ConstantExplanation
inaffect the input sequence
outaffect 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

二次

另见

pubseekposinvokes seekpos() (public member function of std::basic_streambuf)
seekpos virtualrepositions the next pointer in the input sequence, output sequence, or both using absolute addressing (virtual protected member function of std::basic_stringbuf)
seekpos virtualrepositions the file position, using absolute addressing (virtual protected member function of std::basic_filebuf)
seekoff virtualrepositions the next pointer in the input sequence, output sequence, or both, using relative addressing (virtual protected member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/io/strStrebuf/Sekpos