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.cppIf this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow