std::basic_ostream<CharT,Traits>::put
來自 cppreference.com
< cpp | io | basic_ostream
basic_ostream& put( char_type ch ); |
||
作為非格式化輸出函式。在構造並檢查哨兵物件後,將字元 ch
寫入輸出流。
如果輸出因任何原因失敗,則設定 badbit
。
目錄 |
[編輯] 引數
ch | - | 要寫入的字元 |
[編輯] 返回值
*this
[編輯] 注意
與格式化的 operator<< 不同,此函式沒有為 signed char 或 unsigned char 型別過載。
與格式化輸出函式不同,如果輸出失敗,此函式不會設定 failbit
。
[編輯] 示例
執行此程式碼
#include <fstream> #include <iostream> int main() { std::cout.put('a'); // normal usage std::cout.put('\n'); std::ofstream s("/does/not/exist/"); s.clear(); // pretend the stream is good std::cout << "Unformatted output: "; s.put('c'); // this will set badbit, but not failbit std::cout << " fail=" << bool(s.rdstate() & s.failbit); std::cout << " bad=" << s.bad() << '\n'; s.clear(); std::cout << "Formatted output: "; s << 'c'; // this will set badbit and failbit std::cout << " fail=" << bool(s.rdstate() & s.failbit); std::cout << " bad=" << s.bad() << '\n'; }
輸出
a Unformatted output: fail=0 bad=1 Formatted output: fail=1 bad=1
[編輯] 參閱
插入字元資料或插入到右值流中 (函式模板) | |
插入字元塊 (公共成員函式) |