奇怪的字符而不是在WinAPI中使用Unicode的国家字母
Strange characters instead national letters using Unicode in WinAPI
我的程序从文件中读取文本并将其放入组合框中。
当文件包含带有英文字符的文本时,一切正常。
当它包含一些波兰字母时,它们会替换为奇怪的字符。
文件编码为 UTF-8(不带 BOM)。
myCombo = CreateWindowExW(WS_EX_CLIENTEDGE, (LPCWSTR)L"COMBOBOX", NULL,
WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
a, b, c, d,
hwnd, (HMENU)ID_COMBO, hThisInstance, NULL);
wstring foo;
wifstream bar("theTextFile.txt");
getline(bar, foo);
SendMessageW(myCombo, CB_ADDSTRING, (WPARAM)0, (LPARAM)(foo.c_str()));
我该怎么做才能让我的程序显示正确的国家字母?
对不起,我的英语:)很差
默认情况下
,wifstream
不会在Windows上读取UTF-8文本。流语言环境中的codecvt
方面是从文件中的字节转换为 wchar_t
,因此您需要将其设置为将转换为您想要wchar_t
。
像这样:
#include <fstream>
#include <string>
#include <locale> // std::locale
#include <codecvt> // std::codecvt_utf8_utf16
#include <memory> // std::unique_ptr
#include <Windows.h> // WriteConsoleW
int main(int argc, const char * argv[])
{
std::wstring foo;
std::wifstream bar("theTextFile.txt");
typedef std::codecvt_utf8_utf16<wchar_t, 0x10FFFF, std::consume_header> codecvt;
std::unique_ptr<codecvt> ptr(new codecvt);
std::locale utf8_locale((std::locale()), ptr.get());
ptr.release();
bar.imbue(utf8_locale);
std::getline(bar, foo);
DWORD n;
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), foo.c_str(), foo.size(), &n, NULL);
}
相关文章:
- C++ - Unicode Newline
- 如何将unicode字符串从C++传递到delphi
- ascii 和 unicode 在处理级别有什么区别吗?
- 无法从 std::string 中提取C++ Unicode 符号
- MinGW64 - Unicode 源文件错误?
- 使用 Visual Studio 2019 编译 Unicode
- 将ANSI C字符串转换为UNICODE
- C++和带有国家符号的文件路径(也许用 UTF8 编码)
- 如何从 oracle 数据库 c++ 读取 unicode
- 在特定 Unicode 路径中创建文件
- 如何存储 unicode 字符并将其输出到文件?
- C++ wmain 函数错误时使用 Unicode
- C++:打印 Unicode 字符
- QTextStream,用于打印英语和Unicode 16
- 将 unicode 字符串与 c ++ 符号进行比较?
- 如何在C++中使用 UTF-8 和 Unicode?C++20 char8_t有多大?
- 在 CHtmlEditCtrl::SetDocumentHTML 中使用 Unicode
- C++ - 为什么 unicode 输出不正确?
- 奇怪的字符而不是在WinAPI中使用Unicode的国家字母
- UTF-8 Unicode 编码和国家/地区特定字符