名稱空間
變體
操作

std::strstreambuf::seekpos

來自 cppreference.com
< cpp‎ | io‎ | strstreambuf
protected:

virtual pos_type seekpos( pos_type sp,
                          std::ios_base::openmode which =

                              std::ios_base::in | std::ios_base::out );
(C++98 起棄用)
(C++26 中移除)

如果可能,將 std::basic_streambuf::gptr 和/或 std::basic_streambuf::pptr 重新定位到 sp 所指示的位置。

如果在 which 中設定了 std::ios_base::in,則嘗試重新定位 gptr()(獲取區域中的下一個指標)。如果在 which 中設定了 std::ios_base::out,則嘗試重新定位 pptr()(放置區域中的下一個指標)。如果 which 中沒有設定任何位,則操作失敗。

每個下一個指標按以下方式重新定位

  • 如果下一個指標為空,則操作失敗。
  • 否則,透過呼叫 sp.offset() 來確定新的偏移量 newoff (型別為 off_type)。如果 newoff 為負、超出緩衝區範圍或無效,則操作失敗。
  • 否則,將下一個指標賦值,如同 gptr() = eback() + newoffpptr() = pbase() + newoff

目錄

[編輯] 引數

sp - 流位置,例如透過 seekoff()seekpos() 獲得的位置
which - -
常量 解釋
in 影響輸入序列
out 影響輸出序列

[編輯] 返回值

成功時將結果偏移量轉換為 pos_type,失敗時為 pos_type(off_type(-1))

[編輯] 注意

seekpos()std::basic_streambuf::pubseekpos() 呼叫,後者由 std::basic_istream::seekg()std::basic_ostream::seekp() 的單引數版本呼叫。

[編輯] 示例

#include <cstring>
#include <iostream>
#include <strstream>
 
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.

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 55 C++98 seekpos 返回一個未定義的
的非法流位置
pos_type(off_type(-1))
在失敗時返回

[編輯] 另請參閱

[虛擬]
使用相對定址重新定位輸入序列、輸出序列或兩者的下一個指標
(虛擬保護成員函式) [編輯]
[虛擬]
使用絕對定址重新定位輸入序列、輸出序列或兩者的下一個指標
(std::basic_streambuf<CharT,Traits> 的虛擬保護成員函式) [編輯]
[虛擬]
使用絕對定址重新定位輸入序列、輸出序列或兩者的下一個指標
(std::basic_stringbuf<CharT,Traits,Allocator> 的虛擬保護成員函式) [編輯]
[虛擬]
使用絕對地址重新定位檔案位置
(std::basic_filebuf<CharT,Traits> 的虛擬保護成員函式) [編輯]