1

Reproduce: codecvt.cpp

#include <iostream>
#include <locale>
#include <string>
#include <codecvt>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);

    locale utf8(locale(), new codecvt_utf8<wchar_t>);
    wcout.imbue(utf8);

    wstring str(L"Test String OÖ UÜ SŞ iİ ıI");
    wcout << str << '\n';
}

Compile with GCC 6.3.0: g++ codecvt.cpp -o codecvt -O3

Windows CMD: codecvt.exe

Output: Test String O├û U├£ S┼Ş i─░ ─▒I

MSYS2 Bash: $ ./codecvt.exe

Output: Test String O├û U├£ S┼Ş i─░ ─▒I (same)

Ubuntu subsystem for Windows 10 Bash:

Compile with GCC 5.4.0: g++ codecvt.cpp -o codecvt -O3

Execute: ./codecvt

Output: Test String OÖ UÜ SŞ iİ ıI

I've been struggling with wide character problems in c++ lately. I ran into this solution but code only seems to be working in linux environment. (I am a c++ beginner (ish))

I also tried some other solutions:

First solution:

setlocale(LC_ALL, "");

Windows CMD outputs as desired, no problems. But input is erroneous.

MSYS2 Bash does not outputs as desired, but input is correct.

Ubuntu subsystem for Windows 10 Bash is correct with both input and output.

Second solution:

locale::global(locale("C.UTF-8"));

Both Windows CMD and MSYS2 Bash crashes at start of program, MSYS2 Bash also reported:

what(): locale::facet::_S_create_c_locale name not valid

Ubuntu subsystem Bash is still cool, works for input/output correcly.

I am guessing this is a problem related to Windows?

Yahya Gedik
  • 97
  • 1
  • 12
  • two advices: check [this](https://stackoverflow.com/questions/40894874/can-stdcout-work-with-utf-8-on-windows/43957576#43957576) out avoid iostreams, if you can. – C.M. Apr 27 '18 at 21:21
  • Only if I wasn't using `GCC` to develop. I asked this question because I wanted to solve my problems with using C++ standard, not using `Windows.h`, which is not present in `GCC` – Yahya Gedik Apr 27 '18 at 21:51
  • GCC is the right way to go. As I suggested in that post -- exclude console and iostreams from equation first (i.e. redirect output to file and use printf instead of wcout), get it to work, then add console back (set it up with SetConsoleOutputCP() and font), get it to work; then, finally, try using iostreams. – C.M. Apr 28 '18 at 00:08
  • one more thing -- I don't think there is a way to get Unicode input to work with CMD (this console is just too old), but output definitely works (if you set console codepage and appropriate font). – C.M. Apr 28 '18 at 00:10
  • Output is not my only problem, I was making an interactive application. I need to get output to console. I also tried to use `wprintf`, but did not succeed. – Yahya Gedik Apr 28 '18 at 01:46

0 Answers0