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

std::basic_stringbuf::seekoff

性病:基本[医]搜寻:

protected: virtual pos_type seekoff(off_type off, ios_base::seekdir dir, 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并且这个缓冲区是打开的,用于读取%28,也就是说,如果((which & ios_base::in) == ios_base::in%29,然后重新定位读取指针std::basic_streambuf::gptr在GET区域内,如下所述

  • 如果which包括ios_base::out这个缓冲区是为写入%28打开的,也就是说,(which & ios_base::out) == ios_base::out%29,然后重新定位写指针std::basic_streambuf::pptr在PUT区域内,如下所述

  • 如果which包括两者ios_base::inios_base::out并且缓冲器为%28的读写都是开放的。(which & (ios_base::in|ios_base::out)) ==(ios_base::in|ios_base::out)%29,和dirios_base::begios_base::end,然后重新定位读和写指针,如下所述。

  • 否则,此函数将失败。

如果指针%28gptrpptr或者两种%29都被重新定位,其操作如下:

1%29如果要重新定位的指针是空指针和新偏移量newoff如果是非零,则此函数将失败。

2%29新指针偏移量newoff类型off_type已确定

A%29如果dir == ios_base::beg,然后newoff是零

B%29dir == ios_base::cur,然后newoff指针%28的当前位置。gptr()-eback()pptr()-pbase()%29

C%29如果dir == ios_base::end,然后newoff是缓冲区%28的整个初始化部分的长度(如果使用过分配,则高水印指针减去起始指针%29)。

3%29newoff + off < 0%28重新定位将指针移动到缓冲区%29开始之前,或者如果newoff + off如果使用了%29,则该函数将指向缓冲区%28的末尾或缓冲区中的最后一个初始化字符。

4%29否则,指针将被分配为gptr() = eback() + newoff + offpptr() = 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
ConstantExplanation
begthe beginning of a stream
endthe ending of a stream
curthe 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
ConstantExplanation
inaffect the input sequence
outaffect the output sequence

返回值

pos_type(newoff)在成功的时候,pos_type(off_type(-1))论失败与失败[医]类型不能表示结果流位置。

二次

#include <iostream> #include <sstream> int main() { std::stringstream ss("123" // 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 1 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 " << ss.str() << '\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 virtualrepositions the next pointer in the input sequence, output sequence, or both using absolute addressing (virtual protected member function of std::basic_streambuf)
seekoff virtualrepositions the next pointer in the input sequence, output sequence, or both, using relative addressing (virtual protected member function of std::basic_streambuf)
seekoff virtualrepositions the file position, using relative addressing (virtual protected member function of std::basic_filebuf)

© cppreference.com

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

http://en.cppreference.com/w/cpp/io/basic[医]丝杠/衬垫