<cwchar> 是 C++ 标准库中的一个头文件,提供了处理宽字符(wchar_t)和宽字符串的函数。这些函数大部分来自 C 标准库的 <wchar.h>,用于处理宽字符的输入输出、内存操作、字符串操作和其他与宽字符相关的功能。
<cwchar> 头文件常见函数:
宽字符输入输出:
 fgetwc:从文件流中读取一个宽字符。
 fputwc:向文件流中写入一个宽字符。
 fgetws:从文件流中读取一个宽字符串。
 fputws:向文件流中写入一个宽字符串。
宽字符串操作:
 wcscpy:拷贝宽字符串。
 wcslen:获取宽字符串的长度。
 wcscmp:比较两个宽字符串。
 wcsncpy:拷贝指定长度的宽字符串。
宽字符分类和转换:
 iswalpha:判断宽字符是否为字母。
 iswdigit:判断宽字符是否为数字。
 towlower:将宽字符转换为小写。
 towupper:将宽字符转换为大写。
宽字符格式化输入输出:
 wprintf:宽字符格式化输出。
 wscanf:宽字符格式化输入。
 swprintf:将格式化宽字符写入宽字符串。
 swscanf:从宽字符串中读取格式化宽字符。
示例1:
#include <cwchar>
 #include <iostream>
 int main() {
     // 使用 fputws 和 fgetws 进行宽字符串输入输出
     const wchar_t *filename = L"example.txt";
     FILE *file = std::fopen("example.txt", "w");
     if (file) {
         std::fputws(L"Hello, 世界!\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;
 }
示例2:
#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;
 }










