Search code examples
c++unicodeconsolemingw

_O_WTEXT, _O_U16TEXT, _O_U8TEXT - are these modes possible in mingw compiler, are there any workarounds?


#include <fcntl.h>
#include <io.h>
#include <stdio.h>

int main(void) {
  _setmode(_fileno(stdout), _O_U16TEXT);
  wprintf(L"\x043a\x043e\x0448\x043a\x0430 \x65e5\x672c\x56fd\n");
  return 0;
}

returns error at compilation: _O_U16TEXT was not declared in this scope

Is this a show-stopper with this compiler ?


Solution

  • Well, there's a simple workaround: just use values of these constants instead of their names. For example, _O_U16TEXT is 0x00020000 and _O_U8TEXT is 0x00040000.

    I've just confirmed that it works with _setmode using g++ 4.8.1 on Windows 10:

    #include <iostream>
    #include <fcntl.h>
    #include <io.h>
    #include <stdio.h>
    
    int main() {
        _setmode(_fileno(stdout), 0x00020000); // _O_U16TEXT
        std::wcout << L"Русский текст\n";
    }