C++ Standard Library <cwchar>

<cwchar>is a header file in the C++ standard library that provides functions for handling wide characters (wchar_t) and wide strings. Most of these functions come from the C standard library's<wchar.h>Used to handle wide character input/output, memory operations, string operations, and other functions related to wide characters.

Syntax

cwcharThe functions defined in the header file usually have names similar to the standard character handling functions, but withwbeginning, for examplewprintf、wscanfetc. The parameters and return types of these functions also differ from the corresponding standard character functions; they use the wide character typewchar_t。

Basic types

  • wchar_t: a wide character type, used to store wide characters.
  • wint_t: used to store the return value of wide character functions.

Common Functions

1. Wide character input and output

  • fgetwc: Reads a wide character from a file stream.
  • fputwc: Writes a wide character to a file stream.
  • fgetws: Reads a wide string from a file stream.
  • fputws: Writes a wide string to a file stream.

Example

#include <cwchar>
#include <iostream>

int main() {
    // Use fputws and fgetws for wide string input/output
    const wchar_t *filename = L"example.txt";
    FILE *file = std::fopen("example.txt", "w");
    if (file) {
        std::fputws(L"Hello, world!\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;
}

The output result is:

Read from file: Hello, 世界!

2. Wide character and wide string operations

  • wcscpy: Copies a wide string.
  • wcslen: Gets the length of a wide string.
  • wcscmp: Compares wide strings.
  • wcsncpy: Copies a wide string of specified length.

Example

#include <cwchar>
#include <iostream>

int main() {
    wchar_t str1[100] = L"Hello";
    wchar_t str2[100] = L"World";
   
    // Wide string copy
    std::wcscpy(str1, L"Hello, world!");
    std::wcout << L"Copied string: " << str1 << std::endl;

    // Wide string length
    size_t len = std::wcslen(str1);
    std::wcout << L"Length of string: " << len << std::endl;

    // Wide string comparison
    int result = std::wcscmp(str1, str2);
    std::wcout << L"Comparison result: " << result << std::endl;

    // Partial wide string copy
    std::wcsncpy(str2, str1, 5);
    str2[5] = L'\0';  // Ensure it ends with a null character
    std::wcout << L"Partially copied string: " << str2 << std::endl;

    return 0;
}

The output result is:

Copied string: Hello, 

3. Wide character classification and conversion

  • iswalpha: Determines whether a wide character is a letter.
  • iswdigit: Determines whether a wide character is a digit.
  • towlower: Converts a wide character to lowercase.
  • towupper: Converts a wide character to uppercase.

Example

#include <cwchar>
#include <iostream>

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

    // Check if the wide character is a letter
    if (std::iswalpha(ch)) {
        std::wcout << ch << L" is an alphabetic character." << std::endl;
    }

    // Check if the wide character is a digit
    ch = L'9';
    if (std::iswdigit(ch)) {
        std::wcout << ch << L" is a digit." << std::endl;
    }

    // Convert wide character to lowercase
    ch = L'G';
    wchar_t lower_ch = std::towlower(ch);
    std::wcout << L"Lowercase of " << ch << L" is " << lower_ch << std::endl;

    // Convert wide character to uppercase
    ch = L'a';
    wchar_t upper_ch = std::towupper(ch);
    std::wcout << L"Uppercase of " << ch << L" is " << upper_ch << std::endl;

    return 0;
}

The output result is:

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

4. Input/output of wide characters and wide strings

  • wprintf: Wide character formatted output.
  • wscanf: Wide character formatted input.
  • swprintf: writes formatted wide characters to a wide string.
  • swscanf: reads formatted wide characters from a wide string.

Example

#include <cwchar>
#include <iostream>

int main() {
    wchar_t buffer[100];

    // Wide character formatted output
    std::wprintf(L"Formatted output: %d %s\n", 42, L"Hello");

    // Wide character formatted input
    std::wprintf(L"Enter a number and a string: ");
    std::wscanf(L"%d %ls", &buffer);
    std::wprintf(L"You entered: %ls\n", buffer);

    // Write formatted wide characters to a wide string
    std::swprintf(buffer, 100, L"Formatted %d %s", 42, L"output");
    std::wcout << L"Buffer: " << buffer << std::endl;

    // Read formatted wide characters from a wide string
    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;
}

The output result is:

Formatted output: 42 H
Enter a number and a string: example
You entered: 
Buffer: Formatted 42 o
Parsed number: 42, word: o
other extensions