std::strstreambuf::seekoff
來自 cppreference.com
< cpp | io | strstreambuf
protected: virtual pos_type seekoff( off_type off, |
(C++98 起棄用) (C++26 中移除) |
|
如果可能,將 std::basic_streambuf::gptr 和/或 std::basic_streambuf::pptr 重新定位到與緩衝區讀取區域和/或寫入區域的起始、結束或當前位置偏移 off 個字元的位置。
- 如果 which 包含 ios_base::in 且此緩衝區已開啟供讀取,則按照如下所述在讀取區域內重新定位讀取指標 std::basic_streambuf::gptr。
- 如果 which 包含 ios_base::out 且此緩衝區已開啟供寫入,則按照如下所述在寫入區域內重新定位寫入指標 std::basic_streambuf::pptr。
- 如果 which 包含 ios_base::in 和
ios_base::out
且緩衝區已同時開啟供讀取和寫入,並且 way 是 ios_base::beg 或 ios_base::end,則按照如下所述重新定位讀取和寫入指標。 - 否則,此函式失敗。
如果指標(gptr 或 pptr 或兩者)被重新定位,則按以下方式進行
1) 如果要重新定位的指標為空指標,並且新偏移量 newoff 不為零,則此函式失敗。
2) 確定型別為
off_type
的新指標偏移量 newoffa) 如果 way == ios_base::beg,則 newoff 為零
b) 如果 way == ios_base::cur,則 newoff 是指標的當前位置(gptr() - eback() 或 pptr() - pbase())
c) 如果 way == ios_base::end,則 newoff 是緩衝區整個已初始化部分的長度(如果使用超額分配,則是高水位指標減去起始指標)
3) 如果 newoff + off 為負數或超出緩衝區已初始化部分的界限,則函式失敗
4) 否則,指標被賦值,如同透過 gptr() = eback() + newoff + off 或 pptr() = pbase() + newoff + off
目錄 |
[編輯] 引數
off | - | - | ||||||||
way | - | 定義應用相對偏移量的基本位置。它可以是以下常量之一
| ||||||||
which | - | -
|
[編輯] 返回值
成功時返回 pos_type(newoff),失敗時返回 pos_type(off_type(-1)),如果 pos_type 無法表示結果流位置。
[編輯] 示例
執行此程式碼
#include <iostream> #include <strstream> int main() { char a[] = "123"; std::strstream ss(a, sizeof a); // 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 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: '"; std::cout.write(a, sizeof a); std::cout << "'\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)) 在失敗時返回 |
[編輯] 另請參閱
[虛] |
使用絕對定址重新定位輸入序列、輸出序列或兩者的下一個指標 (虛擬保護成員函式) |
[虛] |
使用相對定址重新定位輸入序列、輸出序列或兩者的下一個指標 ( std::basic_streambuf<CharT,Traits> 的虛擬保護成員函式) |
[虛] |
使用相對定址重新定位輸入序列、輸出序列或兩者的下一個指標 ( std::basic_stringbuf<CharT,Traits,Allocator> 的虛擬保護成員函式) |
[虛] |
使用相對地址重新定位檔案位置 ( std::basic_filebuf<CharT,Traits> 的虛擬保護成員函式) |