I have a program that prints UTF-8 string to the console:
#include <stdio.h>
int main()
{
printf("Мир Peace Ειρήνη\n");
return 0;
}
I configure the console to use True Type fonts (Lucida Console), define UTF-8 code-page (chcp 65001) compile this program with both MinGW GCC and Visual Studio 2010 it works perfectly, I see: the output:
Мир Peace Ειρήνη
I do the same using std::cout
#include <iostream>
int main()
{
std::cout << "Мир Peace Ειρήνη\n" ;
return 0;
}
This works perfectly fine as above using MinGW GCC but with Visual Studio 2010 I get squares, more than that the squares (two per each non-ASCII letter).
If I run the program with redirection test >test.txt
I get perfect UTF-8 output
in the file.
Both tests done on Windows 7.
Questions:
- What is the difference between printf and std::cout in the Visual Studio standard library in handling of the output stream - clearly one of them works and other does not?
- How can this be fixed?
Real Answer:
In short: you are screwed - std::cout
does not really work with MSVC + UTF-8 - or at least
requires enormous effort to make it behave reasonably.
In long: read two articles referenced in the answer.