C++ 標準ライブラリ <cwchar>

<cwchar>は C++ 標準ライブラリ内のヘッダーファイルであり、ワイド文字( を処理する関数を提供します。wchar_t)とワイド文字列の関数です。これらの関数の大部分は C 標準ライブラリのもので<wchar.h>、ワイド文字の入出力、メモリ操作、文字列操作、およびその他のワイド文字関連の機能を処理するために使用されます。

構文

cwcharヘッダーファイルで定義される関数は、通常、標準の文字処理関数と似た名前を持ちますが、wで始まり、例えばwprintf、wscanfなどです。これらの関数のパラメータと戻り値の型も対応する標準文字関数とは異なり、ワイド文字型を使用します。wchar_t。

基本型

  • wchar_t:ワイド文字関数の戻り値を格納するために使用されます。
  • wint_tよく使用される関数

よく使用される関数

1. :ファイルストリームからワイド文字を読み取ります。

  • fgetwc:ファイルストリームにワイド文字を書き込みます。
  • fputwc:ファイルストリームからワイド文字列を読み取ります。
  • fgetws:ファイルストリームにワイド文字列を書き込みます。
  • fputws例

例

#include <cwchar>
#include <iostream>

int main() {
    "Hello, 世界!
    const wchar_t *filename = L"example.txt";
    FILE *file = std::fopen("example.txt", "w");
    if (file) {
        std::fputws(L実行結果は次のとおりです:\n", file);
        std::fclose(file);
    }

    file = std::fopen("example.txt", "r");
    if (file) {
        wchar_t buffer[256];
        if (std::fgetws(buffer, 256, file)) {
            std::wcout << L"Read from file: " << buffer;
        }
        std::fclose(file);
    }

    return 0;
}

ワイド文字とワイド文字列の操作

Read from file: Hello, 世界!

2. :ワイド文字列をコピーします。

  • wcscpy:ワイド文字列の長さを取得します。
  • wcslen:ワイド文字列を比較します。
  • wcscmp:指定された長さのワイド文字列をコピーします。
  • wcsncpy例

例

#include <cwchar>
#include <iostream>

int main() {
    wchar_t str1[100] = L"Hello";
    wchar_t str2[100] = L"World";
   
    "Hello, 世界!"
    std::wcscpy(str1, L// ワイド文字列の長さ);
    std::wcout << L"Copied string: " << str1 << std::endl;

    // ワイド文字列の比較
    size_t len = std::wcslen(str1);
    std::wcout << L"Length of string: " << len << std::endl;

    // ワイド文字列の部分コピー
    int result = std::wcscmp(str1, str2);
    std::wcout << L"Comparison result: " << result << std::endl;

    // 必ずヌル文字で終端する
    std::wcsncpy(str2, str1, 5);
    str2[5] = L'\0';  実行結果は次のとおりです:
    std::wcout << L"Partially copied string: " << str2 << std::endl;

    return 0;
}

ワイド文字の分類と変換

Copied string: Hello, 

3. :ワイド文字がアルファベットかどうかを判定します。

  • iswalpha:ワイド文字が数字かどうかを判定します。
  • iswdigit:ワイド文字を小文字に変換します。
  • towlower:ワイド文字を大文字に変換します。
  • towupper例

例

#include <cwchar>
#include <iostream>

int main() {
    wchar_t ch = L'A';

    // ワイド文字が数字かどうかを判定
    if (std::iswalpha(ch)) {
        std::wcout << ch << L" is an alphabetic character." << std::endl;
    }

    // ワイド文字を小文字に変換
    ch = L'9';
    if (std::iswdigit(ch)) {
        std::wcout << ch << L" is a digit." << std::endl;
    }

    // ワイド文字を大文字に変換
    ch = L'G';
    wchar_t lower_ch = std::towlower(ch);
    std::wcout << L"Lowercase of " << ch << L" is " << lower_ch << std::endl;

    実行結果は次のとおりです:
    ch = L'a';
    wchar_t upper_ch = std::towupper(ch);
    std::wcout << L"Uppercase of " << ch << L" is " << upper_ch << std::endl;

    return 0;
}

ワイド文字とワイド文字列の入出力

A is an alphabetic character.
9 is a digit.
Lowercase of G is g
Uppercase of a is A

4. :ワイド文字のフォーマット出力

  • wprintf:ワイド文字のフォーマット入力
  • wscanf:フォーマット済みのワイド文字をワイド文字列に書き込みます。
  • swprintf:ワイド文字列からフォーマット済みのワイド文字を読み取ります。
  • swscanf例

例

#include <cwchar>
#include <iostream>

int main() {
    wchar_t buffer[100];

    // ワイド文字のフォーマット入力
    std::wprintf(L"Formatted output: %d %s\n", 42, L"Hello");

    // フォーマット済みのワイド文字をワイド文字列に書き込む
    std::wprintf(L"Enter a number and a string: ");
    std::wscanf(L"%d %ls", &buffer);
    std::wprintf(L"You entered: %ls\n", buffer);

    // ワイド文字列からフォーマット済みのワイド文字を読み取る
    std::swprintf(buffer, 100, L"Formatted %d %s", 42, L"output");
    std::wcout << L"Buffer: " << buffer << std::endl;

    実行結果は次のとおりです:
    int number;
    wchar_t word[100];
    std::swscanf(buffer, L"Formatted %d %s", &number, word);
    std::wcout << L"Parsed number: " << number << L", word: " << word << std::endl;

    return 0;
}

その他の拡張

Formatted output: 42 H
Enter a number and a string: サンプル
You entered: 
Buffer: Formatted 42 o
Parsed number: 42, word: o
AI 思考中...