Skip to content

Instantly share code, notes, and snippets.

@bredmor
Created November 12, 2021 20:40
Show Gist options
  • Save bredmor/206a36e7ae60bdf0f5a19e94af084641 to your computer and use it in GitHub Desktop.
Save bredmor/206a36e7ae60bdf0f5a19e94af084641 to your computer and use it in GitHub Desktop.

Revisions

  1. bredmor created this gist Nov 12, 2021.
    25 changes: 25 additions & 0 deletions utf8_readln.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #include <cstdio>
    #include <windows.h>
    #define INPUT_MAX 255

    void main()
    {
    wchar_t wstr[INPUT_MAX];
    char mb_str[INPUT_MAX * 3 + 1];
    unsigned long readln;

    SetConsoleOutputCP(CP_UTF8);
    SetConsoleCP(CP_UTF8);

    void *console = GetStdHandle(STD_INPUT_HANDLE);

    ReadConsole(console, wstr, INPUT_MAX, &readln, NULL);

    int size = WideCharToMultiByte(CP_UTF8, 0, wstr, readln, mb_str, sizeof(mb_str), NULL, NULL);

    mb_str[size] = 0;

    printf("echo: %s\n", mb_str);

    return;
    }