通过C++程序在PowerShell中打印unicode字符

bcx*_*000 4 unicode powershell

我的最终目标是通过C++程序将一些非拉丁文本输出写入Windows中的控制台.

cmd.exe让我无处可去,所以我得到了最新的,有光泽的PowerShell版本(支持unicode).我已经证实我可以

  • 输入非unicode字符和
  • 从windows命令看到非unicode控制台输出(如"dir")

例如,我有这个文件,"가.txt"(가是韩语字母表中的第一个字母),我可以得到这样的输出:

PS P:\reference\unicode> dir .\?.txt

    Directory: P:\reference\unicode

Mode                LastWriteTime     Length  
Name                                                       
----                -------------     ------ 
----                                                       
-a---         1/12/2010   8:54 AM          0 ?.txt     
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.但是使用C++程序写入控制台是行不通的.

int main()
{
    wchar_t text[] = {0xAC00, 0}; // ? has code point U+AC00 in unicode
    wprintf(L"%s", text);  // this prints a single question mark: "?"
}
Run Code Online (Sandbox Code Playgroud)

我不知道我错过了什么.我可以输入并在控制台上看到가的事实似乎表明我有三个需要的部分(unicode支持,字体和字形),但我必须弄错.

我也试过"chcp"而没有任何运气.我在C++程序中做错了吗?

谢谢!

Kei*_*ill 8

来自printf文档:

如果以ANSI模式打开流,则wprintf和printf的行为相同.

看看这篇博文.它有这个很好的短小清单:

#include <fcntl.h>
#include <io.h>
#include <stdio.h>

int main(void) {
    _setmode(_fileno(stdout), _O_U16TEXT);
    wprintf(L"\x043a\x043e\x0448\x043a\x0430 \x65e5\x672c\x56fd\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)