std::fwide
來自 cppreference.com
在標頭檔案 <cwchar> 中定義 |
||
int fwide( std::FILE* stream, int mode ); |
||
若 mode > 0,嘗試令 stream 為寬定向。若 mode < 0,嘗試令 stream 為位元組定向。若 mode == 0,則只查詢流的當前定向。
若流的定向已由(透過執行輸出或早先呼叫 fwide)決定,則此函式不做任何事。
目錄 |
[編輯] 引數
stream | - | 指向 C I/O 流的指標,以修改或查詢 |
mode | - | 整數值,大於零以設定流為寬定向,小於零以設定流為窄定向,或零以只查詢 |
[編輯] 返回值
若此呼叫後流為寬定向則返回大於零的整數,若此呼叫後流為位元組定向則返回小於零的整數,若流無定向則返回零。
[編輯] 示例
以下程式碼設定並重置流的定向。
執行此程式碼
#include <cstdio> #include <cstdlib> #include <cwchar> #include <iostream> void show_orientation(int n) { n < 0 ? std::wcout << "\tnarrow orientation\n" : n > 0 ? std::wcout << "\twide orientation\n" : std::wcout << "\tno orientation\n"; } void try_read(FILE* fp) { if (const int c = std::fgetc(fp); c == EOF) std::wcout << "\tnarrow character read failed\n"; else std::wcout << "\tnarrow character read '" << static_cast<char>(c) << "'\n"; if (const wint_t wc = std::fgetwc(fp); wc == WEOF) std::wcout << "\twide character read failed\n"; else std::wcout << "\twide character read '" << static_cast<wchar_t>(wc) << "'\n"; } int main() { enum fwide_orientation : int { narrow = -1, query, wide }; FILE* fp = std::fopen("main.cpp", "r"); if (!fp) { std::wcerr << "fopen() failed\n"; return EXIT_FAILURE; } std::wcout << "1) A newly opened stream has no orientation.\n"; show_orientation(std::fwide(fp, fwide_orientation::query)); std::wcout << "2) Establish byte orientation.\n"; show_orientation(std::fwide(fp, fwide_orientation::narrow)); try_read(fp); std::wcout << "3) Only freopen() can reset stream orientation.\n"; if (std::freopen("main.cpp", "r", fp) == NULL) { std::wcerr << "freopen() failed\n"; return EXIT_FAILURE; } std::wcout << "4) A reopened stream has no orientation.\n"; show_orientation(std::fwide(fp, fwide_orientation::query)); std::wcout << "5) Establish wide orientation.\n"; show_orientation(std::fwide(fp, fwide_orientation::wide)); try_read(fp); std::fclose(fp); }
可能的輸出
1) A newly opened stream has no orientation. no orientation 2) Establish byte orientation. narrow orientation narrow character read '#' wide character read failed 3) Only freopen() can reset stream orientation. 4) A reopened stream has no orientation. no orientation 5) Establish wide orientation. wide orientation narrow character read failed wide character read '#'
[編輯] 參閱
開啟檔案 (函式) | |
C 文件 關於 fwide
|