std::fgetws
來自 cppreference.com
在標頭檔案 <cwchar> 中定義 |
||
wchar_t* fgetws( wchar_t* str, int count, std::FILE* stream ); |
||
從給定的檔案流中讀取最多 count - 1 個寬字元,並將它們儲存在 str 中。生成的寬字串總是以空字元結尾。如果遇到檔案結束或發現換行寬字元,則停止解析,在這種情況下 str 將包含該寬換行字元。
目錄 |
[編輯] 引數
str | - | 用於讀取字元的寬字串 |
count | - | str 的長度 |
stream | - | 從中讀取資料的檔案流 |
[編輯] 返回值
成功時返回 str,錯誤時返回空指標。
[編輯] 示例
執行此程式碼
#include <array> #include <clocale> #include <cstdio> #include <cstdlib> #include <cwchar> #include <cwctype> #include <iomanip> #include <iostream> #include <span> #include <string> void dump(std::span<const wchar_t> sp, std::size_t width = 14) { for (wchar_t wc : sp) std::wcout << (std::iswprint(wc) ? wc : L'.'); std::wcout << std::wstring(width > sp.size() ? width - sp.size() : 1, L' ') << std::hex << std::uppercase << std::setfill(L'0'); for (wchar_t wc : sp) std::wcout << std::setw(sizeof wc) << static_cast<unsigned>(wc) << ' '; std::wcout << '\n'; } int main() { // Create temp file that contains wide characters std::setlocale(LC_ALL, "en_US.utf8"); std::FILE* tmpf = std::tmpfile(); for (const wchar_t* text : { L"Tétraèdre" L"\n", L"Cube" L"\n", L"Octaèdre" L"\n", L"Icosaèdre" L"\n", L"Dodécaèdre" L"\n" }) if (int rc = std::fputws(text, tmpf); rc == EOF) { std::perror("fputws()"); // POSIX requires that errno is set return EXIT_FAILURE; } std::rewind(tmpf); std::array<wchar_t, 12> buf; while (std::fgetws(buf.data(), buf.size(), tmpf) != nullptr) dump(std::span(buf.data(), buf.size())); return EXIT_SUCCESS; }
可能的輸出
Tétraèdre... 0054 00E9 0074 0072 0061 00E8 0064 0072 0065 000A 0000 0000 Cube..dre... 0043 0075 0062 0065 000A 0000 0064 0072 0065 000A 0000 0000 Octaèdre.... 004F 0063 0074 0061 00E8 0064 0072 0065 000A 0000 0000 0000 Icosaèdre... 0049 0063 006F 0073 0061 00E8 0064 0072 0065 000A 0000 0000 Dodécaèdre.. 0044 006F 0064 00E9 0063 0061 00E8 0064 0072 0065 000A 0000
[編輯] 參閱
從 stdin、檔案流或緩衝區讀取格式化的寬字元輸入 (函式) | |
從檔案流中獲取一個寬字元 (函式) | |
向檔案流寫入一個寬字串 (函式) | |
C 文件 用於 fgetws
|