How to fix std::wcout printing question marks (?) on Linux
Problem:
You are trying to print a wstring
from a wstring
literal using std::wcout
(with an UTF-8-encoded source file):
wstring w = L"Test: äöü";
wcout << w << endl;
but when you run this program, you see
Test: ???
Solution
Use setlocale()
to set a UTF-8 locale:
setlocale( LC_ALL, "en_US.utf8" );
wstring w = L"Test: äöü";
wcout << w << endl;
This will print
Test: äöü
as expected.
Full example
#include <string>
#include <iostream>
using namespace std;
int main() {
setlocale( LC_ALL, "en_US.utf8" );
wstring w = L"Test: äöü";
wcout << w << endl;
}
Compile like this:
g++ -o main main.cpp