std::basic_ostream<CharT,Traits>::seekp
來自 cppreference.com
< cpp | io | basic_ostream
basic_ostream& seekp( pos_type pos ); |
(1) | |
basic_ostream& seekp( off_type off, std::ios_base::seekdir dir ); |
(2) | |
設定當前關聯的 streambuf
物件的輸出位置指示器。
行為類似於 UnformattedOutputFunction(除了實際上不執行輸出)。在構造並檢查哨兵物件之後: |
(C++11 起) |
1) 如果 fail() != true,透過呼叫 rdbuf()->pubseekpos(pos, std::ios_base::out) 將輸出位置指示器設定為絕對(相對於檔案開頭)值 pos。如果失敗,則呼叫 setstate(std::ios_base::failbit)。
2) 如果 fail() != true,透過呼叫 rdbuf()->pubseekoff(off, dir, std::ios_base::out) 將輸出位置指示器設定為相對於 dir 的偏移量 off。如果失敗,則呼叫 setstate(std::ios_base::failbit)。
目錄 |
[編輯] 引數
pos | - | 要設定的輸出位置指示器的絕對位置 | ||||||||
off | - | 要設定的輸出位置指示器的相對位置(正或負) | ||||||||
dir | - | 定義應用相對偏移量的基本位置。它可以是以下常量之一
|
[編輯] 返回值
*this
[編輯] 異常
[編輯] 示例
執行此程式碼
#include <iostream> #include <sstream> int main() { std::ostringstream os("hello, world"); os.seekp(7); os << 'W'; os.seekp(0, std::ios_base::end); os << '!'; os.seekp(0); os << 'H'; std::cout << os.str() << '\n'; }
輸出
Hello, World!
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 129 | C++98 | 沒有辦法指示失敗 | 失敗時設定 failbit |
LWG 136 | C++98 | seekp 可能會設定輸入流 |
僅設定輸出流 |
LWG 537 | C++98 | 1. pos 的型別為 pos_type& 2. off 的型別為 off_type& |
1. 修正為 pos_type 2. 修正為 off_type |
LWG 2341 | C++98 | 已移除 LWG issue 129 對過載 (2) 的解決方案 | 恢復 |
[編輯] 參見
返回輸出位置指示器 (公有成員函式) | |
返回輸入位置指示符 ( std::basic_istream<CharT,Traits> 的公有成員函式) | |
設定輸入位置指示符 ( std::basic_istream<CharT,Traits> 的公有成員函式) |