名稱空間
變體
操作

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::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() 的單引數版本會呼叫 std::basic_streambuf::pubseekpos()

[編輯] 示例

#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> 的公有成員函式) [編輯]
[虛擬函式]
使用相對定址重新定位輸入序列、輸出序列或兩者的下一個指標
(虛保護成員函式) [編輯]
[虛擬函式]
使用絕對地址重新定位檔案位置
(std::basic_filebuf<CharT,Traits> 的虛保護成員函式) [編輯]
[虛擬函式]
使用絕對定址重新定位輸入序列、輸出序列或兩者的下一個指標
(std::strstreambuf 的虛保護成員函式) [編輯]