Namespaces
Variants

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

From cppreference.net
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 指示的位置。

有效地执行 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.

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用标准 发布时行为 正确行为
LWG 375 C++98 std::ios_base 的静态常量成员被
错误指定为 std::basic_ios 的成员
已修正
LWG 564 C++98 未明确说明如何重定位 gptr 和/或 pptr 通过 seekoff() 进行重定位

参见

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