Output unicode strings in Windows console app

后端 未结 11 944
花落未央
花落未央 2020-11-21 11:20

Hi I was trying to output unicode string to a console with iostreams and failed.

I found this: Using unicode font in c++ console app and this snippet work

11条回答
  •  旧巷少年郎
    2020-11-21 12:07

    The wcout must have the locale set differently to the CRT. Here's how it can be fixed:

    int _tmain(int argc, _TCHAR* argv[])
    {
        char* locale = setlocale(LC_ALL, "English"); // Get the CRT's current locale.
        std::locale lollocale(locale);
        setlocale(LC_ALL, locale); // Restore the CRT.
        std::wcout.imbue(lollocale); // Now set the std::wcout to have the locale that we got from the CRT.
        std::wcout << L"¡Hola!";
        std::cin.get();
        return 0;
    }
    

    I just tested it, and it displays the string here absolutely fine.

提交回复
热议问题