Behebung von std::wcout druckt Fragezeichen (?) auf Linux

English Deutsch

Problem:

Du versuchst, einen wstring aus einem wstring-Literal mit std::wcout auszugeben (mit einer UTF-8-kodierten Quelldatei):

wstring_example.cpp
wstring w = L"Test: äöü";
wcout << w << endl;

aber wenn du dieses Programm ausführst, siehst du

wcout_output_questionmarks.txt
Test: ???

Lösung

Use setlocale() to set a UTF-8 locale:

wcout_locale_example.cpp
setlocale( LC_ALL, "en_US.utf8" );
wstring w = L"Test: äöü";
wcout << w << endl;

Dies wird ausgeben

wcout_output_utf8.txt
Test: äöü

wie erwartet.

Vollständiges Beispiel

wcout_full_example.cpp
#include <string>
#include <iostream>

using namespace std;

int main() {
    setlocale( LC_ALL, "en_US.utf8" );
    wstring w = L"Test: äöü";
    wcout << w << endl;
}

Kompiliere so:

build_wcout_example.sh
g++ -o main main.cpp

Check out similar posts by category: C/C++, Linux