【C++基础]007_char、wchar_t、wcout、setlocale() 解决中文不能输出显示

本文详细介绍了C++中char、wchar_t、wcout的基本使用,并通过设置区域化代码实现中文输出,对比了不同字符类型及输出方式的特性。

【C++基础]007_char、wchar_t、wcout、setlocale()

 
复制代码
 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main(){
 5     char ch1 = 'A';
 6     cout<<"ch1 = "<<ch1<<endl;
 7 
 8     char ch2 = '';
 9     cout<<"ch2 = "<<ch2<<endl;
10 
11     wchar_t ch3 = '';
12     cout<<"ch3 = "<<ch3<<endl;
13 
14     wchar_t ch4[] = L"";
15     cout<<"ch4 = "<<ch4<<endl;
16 
17     system("pause");
18     return 0;
19 }
复制代码

上面的代码会输出什么呢?自己看看再看下面的答案:

ch1 = A
ch2 =
ch3 = 54992
ch4 = 0043F944
请按任意键继续. . .

我去!为什么输不了中文呢??

那要怎么输出呢!看下面的代码吧!

复制代码
 1 #include<iostream>
 2 #include<locale>
 3 using namespace std;
 4 
 5 int main(){
 6     char ch1 = 'A';
 7     cout<<"ch1 = "<<ch1<<endl;
 8 
 9     char ch2 = '';
10     cout<<"ch2 = "<<ch2<<endl;
11 
12     wchar_t ch3 = '';
13     cout<<"ch3 = "<<ch3<<endl;
14 
15     setlocale(LC_ALL, "chs");
16     wchar_t ch4[] = L"";
17     wcout<<"ch4 = "<<ch4<<endl;
18 
19     system("pause");
20     return 0;
21 }
复制代码

注意,我在最后一段输出里面加上了一句设置区域化的代码,代码被设置在中文环境中。同时,我用了wcout来输出中文。

上面的程序输出如下所示:

1 ch1 = A
2 ch2 =
3 ch3 = 54992
4 ch4 =5 请按任意键继续. . .

没有java和c#方便啊!不过挺好玩的,呵呵!

总结:

处理双字节字符方法如下

1. 设置区域,用setlocale()方法

2. 定义字符变量,方法见代码

3. 用wcout输出

转载于:https://www.cnblogs.com/xiao-wei-wei/archive/2013/03/18/2965749.html

#include <windows.h> #include <iostream> #include <tchar.h> #include <vector> #include <string> #include <thread> #include <filesystem> namespace fs = std::filesystem; void 监控文件夹(LPCWSTR szDir) { HANDLE hDir = CreateFileW( szDir, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL ); if (hDir == INVALID_HANDLE_VALUE) { std::wcerr << L"无法打开目录: " << szDir << std::endl; return; } BYTE buffer[4096]; // 增大缓冲区以避免溢出 DWORD dwBytesReturned; std::wcout << L"开始监控目录: " << szDir << std::endl; while (true) { if (ReadDirectoryChangesW( hDir, buffer, sizeof(buffer), TRUE, // fWatchSubtree = TRUE,监控子目录 FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SECURITY, &dwBytesReturned, NULL, NULL )) { FILE_NOTIFY_INFORMATION* pNotify = (FILE_NOTIFY_INFORMATION*)buffer; do { std::wstring fullPath = szDir; fullPath += L"\\" + std::wstring(pNotify->FileName, pNotify->FileNameLength / sizeof(WCHAR)); switch (pNotify->Action) { case FILE_ACTION_ADDED: std::wcout << L"[添加] " << fullPath << std::endl; break; case FILE_ACTION_REMOVED: std::wcout << L"[删除] " << fullPath << std::endl; break; case FILE_ACTION_MODIFIED: std::wcout << L"[修改] " << fullPath << std::endl; break; case FILE_ACTION_RENAMED_OLD_NAME: std::wcout << L"[重命名旧名] " << fullPath << std::endl; break; case FILE_ACTION_RENAMED_NEW_NAME: std::wcout << L"[重命名新名] " << fullPath << std::endl; break; default: std::wcout << L"[未知操作] " << fullPath << std::endl; break; } pNotify = (FILE_NOTIFY_INFORMATION*)((BYTE*)pNotify + pNotify->NextEntryOffset); } while (pNotify->NextEntryOffset != 0); } else { std::wcerr << L"ReadDirectoryChangesW 失败,错误代码: " << GetLastError() << std::endl; break; } } CloseHandle(hDir); } void 监控所有子目录(LPCWSTR szRootDir) { std::vector<std::thread> threads; // 主线程监控根目录 threads.emplace_back(监控文件夹, szRootDir); // 遍历所有子目录,每个子目录启动一个线程进行监控 for (const auto& dirEntry : fs::recursive_directory_iterator(szRootDir)) { if (dirEntry.is_directory()) { Sleep(1000); try { std::wstring subDir = dirEntry.path().wstring(); threads.emplace_back(监控文件夹, subDir.c_str()); } catch (const std::exception& ex) { std::cerr << "无法监控子目录: " << ex.what() << std::endl; } } } // 等待所有线程结束(实际上不会结束,除非手动中断) for (auto& t : threads) { if (t.joinable()) { t.join(); } } } int main() { setlocale(LC_ALL, "chs"); // 启用宽字符支持 WCHAR szStrFiles[PATH_MAX] = L"E:\\Toolshed"; std::wcout << L"开始递归监控:" << szStrFiles << std::endl; 监控所有子目录(szStrFiles); return 0; } 请优化多线程冲突的问题,添加检查和管理员运行,使用宏支持Win32和Linux环境
08-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值