在 Windows 中使用 C++ 将 Unicode 输出到控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2849010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:15:53  来源:igfitidea点击:

Output Unicode to console Using C++, in Windows

c++unicodewindows-xpconsole

提问by Jesse Foley

I'm still learning C++, so bear with me and my sloppy code. The compiler I use is Dev C++. I want to be able to output Unicode characters to the Console using cout. Whenver i try things like:

我还在学习 C++,所以请忍受我和我草率的代码。我使用的编译器是 Dev C++。我希望能够使用 cout 将 Unicode 字符输出到控制台。每当我尝试以下操作时:

#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
    std::cout << "Blah blah blah some gibberish unicode: ?????\n";
    system("PAUSE");
    return 0;
}

It outputs strange characters to the console, like μA■Gg. Why does it do that, and how can I get to to display ?????? Or is this not possible with Windows?

它向控制台输出奇怪的字符,例如 μA■Gg。为什么会这样,我如何才能显示??????或者这在 Windows 上是不可能的?

回答by Tyn

What about std::wcout?

怎么样std::wcout

#include <iostream>

int main() {
    std::wcout << L"Hello World!" << std::endl;
    return 0;
}

This is the standard wide-characters output stream.

这是标准的宽字符输出流。

Still, as Adrian pointed out, this doesn't address the fact cmd, by default, doesn't handle Unicode outputs. This can be addressed by manually configuring the console, like described in Adrian's answer:

尽管如此,正如 Adrian 指出的那样,这并没有解决cmd默认情况下不处理 Unicode 输出的事实。这可以通过手动配置控制台来解决,如 Adrian 的回答中所述:

  • Starting cmdwith the /uargument;
  • Calling chcp 65001to change the output format;
  • And setting a unicode font in the console (like Lucida Console Unicode).
  • 启动cmd/u论证;
  • 调用chcp 65001改变输出格式;
  • 并在控制台中设置 unicode 字体(如 Lucida Console Unicode)。

You can also try to use _setmode(_fileno(stdout), _O_U16TEXT);, which require fcntl.hand io.h(as described in this answer, and documented in this blog post).

您还可以尝试使用_setmode(_fileno(stdout), _O_U16TEXT);, 需要fcntl.hio.h(如本答案所述,并记录在本博客文章中)。

回答by Adrian McCarthy

I'm not sure Windows XP will fully support what you need. There are three things you have to do to enable Unicode with a command console:

我不确定 Windows XP 是否会完全支持您所需要的。要使用命令控制台启用 Unicode,您必须做三件事:

  1. Start the command window with cmd /u. The /usays your programs will output Unicode.
  2. Use chcp 65001to indicate you want to use UTF-8 instead of one of the code pages.
  3. Select a font with more glyph coverage. The command windows in newer versions of Windows offer Lucida Console Unicode. My XP box has a subset of that called Lucida Console. It doesn't have a very extensive reperttheitroade, but it should be sufficient if you're just trying to display some accented characters.
  1. 用 启动命令窗口cmd /u。该/u说你的程序将输出的Unicode。
  2. 使用chcp 65001指示要使用UTF-8,而不是代码页中的一个。
  3. 选择具有更多字形覆盖的字体。较新版本的 Windows 中的命令窗口提供Lucida Console Unicode. 我的 XP 盒子有一个叫做Lucida Console. 它没有非常广泛的曲目,但如果您只是想显示一些重音字符,它应该足够了。

回答by Puppy

You used the ANSI output stream. You need to use
std::wcout << L"Blah blah blah some gibberish unicode: ?????\n";

您使用了 ANSI 输出流。你需要使用
std::wcout << L"Blah blah blah some gibberish unicode: ?????\n";

Also, use std::cin.get(), not system("PAUSE")

另外,使用 std::cin.get(),而不是 system("PAUSE")

回答by quanta

In Linux, I can naively do:

在 Linux 中,我可以天真地做:

std::cout << "? , Α, Β, Γ, Δ, ,Θ , Λ, Ξ, ... ±, ... etc";

and it worked for most of the characters I tried.

它适用于我尝试过的大多数角色。