std::basic_stringbuf<CharT,Traits,Allocator>::seekoff
來自 cppreference.com
< cpp | io | basic stringbuf
protected: virtual pos_type seekoff( off_type off, |
||
若可能,將 std::basic_streambuf::gptr 和/或 std::basic_streambuf::pptr 重定位到精確地從緩衝區的獲取區(get area)和/或放置區(put area)的起始、結尾或當前位置偏移 off 個字元的位置。
- 若 which 包含 std::ios_base::in 且此緩衝區為讀取而開啟(即,若 (which & std::ios_base::in) == std::ios_base::in),則如下文所述在獲取區內重定位讀指標 std::basic_streambuf::gptr
- 若 which 包含 std::ios_base::out 且此緩衝區為寫入而開啟(即,(which & std::ios_base::out) == std::ios_base::out),則如下文所述在放置區內重定位寫指標 std::basic_streambuf::pptr
- 若 which 同時包含 std::ios_base::in 和 std::ios_base::out 且緩衝區為讀寫而開啟(即,(which & (std::ios_base::in | std::ios_base::out)) == (std::ios_base::in | std::ios_base::out)),且 dir 為 std::ios_base::beg 或 std::ios_base::end,則如下文所述重定位讀寫指標二者。
- 否則,此函式失敗。
1) 確定型別為
off_type
的新指標偏移 newoff2) 若要重定位的指標是空指標且 newoff 不為零,則此函式失敗。
4) 否則,如同透過 gptr() = eback() + newoff + off 或 pptr() = pbase() + newoff + off 賦值該指標。
目錄 |
[編輯] 引數
off | - | - | ||||||||
dir | - | 定義應用相對偏移量的基本位置。它可以是以下常量之一
| ||||||||
which | - | -
|
[編輯] 返回值
成功時為 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 的虛保護成員函式) |