You must use Unicode to specify the characters you want to display. The character represented by byte 02h in the console is converted by codepage 437 ( cp437 ) to the Unicode character U+263B . Using the source file stored in UTF-8 using the specification simplifies the use of Unicode, since you can insert or enter the desired characters without resorting to Unicode escape codes.
For a file stream, the stream must be configured for UTF-8. There are various ways to do this, and it depends on the compiler, but using Visual Studio 2012, a source stored in UTF-8 w / BOM, and a bit of Googling:
#include <locale> #include <codecvt> #include <fstream> #include <iostream> #include <io.h> #include <fcntl.h> using namespace std; int main() { const std::locale utf8_locale = std::locale(std::locale(), new std::codecvt_utf8<wchar_t>()); wofstream f(L"sample.txt"); f.imbue(utf8_locale); f << L"\u263bζζ―ηΎε½δΊΊγζε«ι©¬ε
γ" << endl; _setmode(_fileno(stdout),_O_U16TEXT); wcout << L"\u263bζζ―ηΎε½δΊΊγζε«ι©¬ε
γ" << endl; }
The contents of sample.txt , as shown in Notepad:
β»ζζ―ηΎε½δΊΊγζε«ι©¬ε
γ
Hex dump (correct UTF-8):
E68891E698AFE7BE8EE59BBDE4BABAE38082E68891E58FABE9A9ACE5858BE380820D0A
Pull out to the console cut and paste here. There was a visual display for every Chinese character without the correct font, but the characters displayed correctly inserted into SO or Notepad.
β»ζζ―ηΎε½δΊΊγζε«ι©¬ε
γ
source share