名稱空間
變體
操作

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

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

virtual pos_type seekoff( off_type off,
                          std::ios_base::seekdir dir,

                          std::ios_base::openmode which = std::ios_base::in | std::ios_base::out );

若可能,將 std::basic_streambuf::gptr 和/或 std::basic_streambuf::pptr 重定位到精確地從緩衝區的獲取區(get area)和/或放置區(put area)的起始、結尾或當前位置偏移 off 個字元的位置。

若重定位 gptr 和/或 pptr,則按如下進行

1) 確定型別為 off_type 的新指標偏移 newoff
a)dir == std::ios_base::beg,則 newoff 為零
b)dir == std::ios_base::cur,則 newoff 是指標的當前位置(gptr() - eback()pptr() - pbase()
c)dir == std::ios_base::end,則 newoff 是緩衝區整個初始化部分的長度(若使用過分配,則為高水位指標減去起始指標)
2) 若要重定位的指標是空指標且 newoff 不為零,則此函式失敗。
3)newoff + off < 0 (重定位會將指標移動到緩衝區起始之前)或若 newoff + off 會指向緩衝區結尾之後(或若使用過分配,則為最後一個已初始化字元之後),則函式失敗。
4) 否則,如同透過 gptr() = eback() + newoff + offpptr() = pbase() + newoff + off 賦值該指標。

目錄

[編輯] 引數

off - -
dir - 定義應用相對偏移量的基本位置。它可以是以下常量之一
常量 解釋
beg 流的開始
end 流的結束
cur 流位置指示器的當前位置
which - -
常量 解釋
in 影響輸入序列
out 影響輸出序列

[編輯] 返回值

成功時為 pos_type(newoff),失敗或 pos_type 不能表示結果流位置時為 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'

[編輯] 缺陷報告

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

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 55 C++98 seekoff 失敗時返回未定義
的非法流位置
pos_type(off_type(-1))
在失敗時返回
LWG 375 C++98 std::ios_base 的靜態常量成員被
錯誤地指定為 std::basic_ios 的成員
已更正
LWG 432 C++98 即使 newoff + off 會指向
最後一個已初始化字元之後,seekoff 也可能成功
seekoff 失敗
在這種情況下
LWG 453 C++98 以零的新偏移重定位空 gptr() 和/或空 pptr()
總是失敗
在這種情況下可以成功
LWG 563 C++98 結束指標不能用於計算 newoff,因為它在解決 LWG issue 432
不能被程式精確控制
改用高水位
指標

[編輯] 參閱

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