std::basic_ios<CharT,Traits>::clear
來自 cppreference.com
void clear( std::ios_base::iostate state = std::ios_base::goodbit ); |
||
透過將流錯誤狀態標誌賦值為 state 的值來設定它們。預設情況下,賦值為 std::ios_base::goodbit,這將清除所有錯誤狀態標誌。
如果 rdbuf() 是空指標(即沒有關聯的流緩衝區),則賦值為 state | std::ios_base::badbit。
目錄 |
[編輯] 引數
state | - | 新的錯誤狀態標誌設定。它可以是以下常量的組合
|
[編輯] 返回值
(無)
[編輯] 異常
如果新的錯誤狀態包含的位也包含在 exceptions() 掩碼中,則丟擲型別為 failure 的異常。
[編輯] 示例
不帶引數的 clear()
可用於在意外輸入後取消設定 failbit
;對於 std::cin.putback(c),請參閱 ungetc
。
執行此程式碼
#include <iostream> #include <string> int main() { for (char c : {'\n', '4', '1', '.', '3', '\n', 'Z', 'Y', 'X'}) std::cin.putback(c); // emulate user's input (not portable: see ungetc Notes) double n; while (std::cout << "Please, enter a number: " && !(std::cin >> n)) { std::cin.clear(); std::string line; std::getline(std::cin, line); std::cout << line << "\nI am sorry, but '" << line << "' is not a number\n"; } std::cout << n << "\nThank you for entering the number " << n << '\n'; }
輸出
Please, enter a number: XYZ I am sorry, but 'XYZ' is not a number Please, enter a number: 3.14 Thank you for entering the number 3.14
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 412 | C++98 | 如果當前錯誤狀態 包含的位也包含在 exceptions() 掩碼中,則會丟擲異常 |
檢查新的 錯誤狀態 |
[編輯] 另請參閱
設定狀態標誌 (公共成員函式) | |
返回狀態標誌 (公共成員函式) |