Reading UTF-8 characters from console

后端 未结 2 438
萌比男神i
萌比男神i 2021-01-12 19:46

I\'m trying to read UTF-8 encoded polish characters from console for my c++ application. I\'m sure that console uses this code page (checked in properties). What I have alre

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-12 20:00

    Here is the trick I use for UTF-8 support. The result is multibyte string which could be then used elsewhere:

    #include 
    #include 
    #define MAX_INPUT_LENGTH 255
    
    int main()
    {
    
        SetConsoleOutputCP(CP_UTF8);
        SetConsoleCP(CP_UTF8);
    
        wchar_t wstr[MAX_INPUT_LENGTH];
        char mb_str[MAX_INPUT_LENGTH * 3 + 1];
    
        unsigned long read;
        void *con = GetStdHandle(STD_INPUT_HANDLE);
    
        ReadConsole(con, wstr, MAX_INPUT_LENGTH, &read, NULL);
    
        int size = WideCharToMultiByte(CP_UTF8, 0, wstr, read, mb_str, sizeof(mb_str), NULL, NULL);
        mb_str[size] = 0;
    
        std::printf("ENTERED: %s\n", mb_str);
    
        return 0;
    }
    

    Should look like this:

    P.S. Big thanks to Remy Lebeau for pointing out some flaws!

提交回复
热议问题