Reputation: 6432
My program reads text from a file and puts it in combo box.
When the file contains text with english characters everything works fine.
When it contains some polish letters, they are replacing with strange characters.
The file encoding is UTF-8 (without 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()));
What can I do to make my program showing correct national letters?
Upvotes: 2
Views: 735
Reputation: 88155
wifstream
won't read UTF-8 text on Windows by default. The codecvt
facet in the stream's locale is what converts from the bytes in the file into wchar_t
, so you need to set this such that it will do the conversion to wchar_t
you want.
Something like this:
#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);
}
Upvotes: 3