Namespaces
Variants

std:: fgetws

From cppreference.net
< cpp ‎ | io ‎ | c
ヘッダーで定義 <cwchar>
wchar_t * fgetws ( wchar_t * str, int count, std:: FILE * stream ) ;

指定されたファイルストリームから最大 count - 1 個のワイド文字を読み取り、 str に格納します。生成されるワイド文字列は常にnull終端されます。ファイル終端が発生した場合、または改行ワイド文字が見つかった場合(この場合 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()
{
    // ワイド文字を含む一時ファイルを作成
    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ではerrnoが設定されることを要求
            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 、ファイルストリーム、またはバッファから行う
(関数)
ファイルストリームからワイド文字を取得する
(関数)
ワイド文字列をファイルストリームに書き込む
(関数)