CppDS.com

C++ 98 11 14 17 20 手册

std::basic_stringbuf<CharT,Traits,Allocator>::seekpos

来自cppreference.com
< cpp‎ | io‎ | basic stringbuf
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::pptrsp 所指示的位置。

等效地执行 seekoff(off_type(sp), std::ios_base::beg, which)

参数

sp - 流位置,例如由 seekoff()seekpos() 获得者
which - 定义影响的是输入序列、输出序列或两者。它能为下列常量之一或其组合:
 
常量 解释
in 影响输入序列
out 影响输出序列

返回值

成功时为 sp ,失败时为 pos_type(off_type(-1))

注意

seekpos()std::basic_streambuf::pubseekpos() 所调用,它又为 std::basic_istream::seekg()std::basic_ostream::seekp() 的单参数版本所调用。

示例

#include <sstream>
#include <iostream>
 
struct mybuf : std::stringbuf
{
    mybuf(const std::string& str) : std::stringbuf(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::stringbuf::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

参阅

调用 seekpos()
(std::basic_streambuf<CharT,Traits> 的公开成员函数)
用相对寻址,重定位输入序列、输出序列或两者中的下一位置指针
(虚受保护成员函数)
用绝对寻址重寻位文件位置
(std::basic_filebuf<CharT,Traits> 的虚受保护成员函数)
用绝对寻址重寻位输入序列、输出序列或两者中的下一位置指针
(std::strstreambuf 的虚受保护成员函数)
关闭