wcout不将宽字符写出到命令提示符 [英] wcout not writing wide character out to command prompt

查看:112
本文介绍了wcout不将宽字符写出到命令提示符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Windows命令提示符下写出以下字符:ュ(U + FF6D).

I am attempting to write the following character out in windows command prompt: ュ (U+FF6D).

我可以看到使用WriteConsoleW将字符写出.如果我在CP_ACP代码页上使用WideCharToMultiByte,我也可以看到该字符(chcp返回932:日语).但是,当我尝试在WriteConsoleW成功打印的同一字符串上使用常规wcout时,它会阻塞.

I am able to see the character get written out using WriteConsoleW. I am also able to see the character if i use WideCharToMultiByte using the CP_ACP code page (chcp returns 932: Japanese). However, when I attempt to use regular wcout on the same string which WriteConsoleW successfully prints, it chokes.

当我执行setlocale(LC_ALL,")时,它会打印English_UnitedStates.1252(安装时的默认代码页).

When I execute setlocale(LC_ALL, "") it prints English_UnitedStates.1252 (the default code page that I had when I installed).

当其他成功时,为什么wcout失败了?

Why is wcout failing when the others are succeeding?

注意:我重新启动了计算机,将其系统区域设置更改为Japan Japanese

推荐答案

C ++ iostream的默认语言环境始终是"C".语言环境.根据C ++ 03标准§27.4.2.3/4:

The default locale for C++ iostreams is always the "C" locale. From the C++03 standard, §27.4.2.3/4:

locale getloc() const;

如果未插入任何语言环境,则在构建时有效的是全局C ++语言环境的副本locale().

If no locale has been imbued, a copy of the global C++ locale, locale(), in effect at the time of construction.

摘自§22.1.1.2/1-2:

From §22.1.1.2/1-2:

locale() throw();

默认构造函数:当前全局语言环境的快照.

Default constructor: a snapshot of the current global locale.

构造上次传递给locale::global(locale&)的参数的副本(如果已调用);否则,生成的构面具有与locale::classic()相同的虚函数语义.

Constructs a copy of the argument last passed to locale::global(locale&), if it has been called; else, the resulting facets have virtual function semantics identical to those of locale::classic().

摘自§22.1.1.5/4-6:

From §22.1.1.5/4-6:

static const locale& classic();

"C"表示区域设置.

返回:实现传统的"C"语言环境的语言环境.语言环境语义,等效于值locale("C").

Returns: A locale that implements the classic "C" locale semantics, equivalent to the value locale("C").

注意:此语言环境,其构面及其成员功能不会随时间变化.

Notes: This locale, its facets, and their member functions, do not change with time.

由于std::coutstd::wcout具有静态存储持续时间,因此可以保证在main被调用之前对其进行初始化,因此 始终具有"C".应用程序启动时的语言环境;即,没有足够的执行时间来调用locale::global并更改std::coutstd::wcout的默认语言环境.因此,如果要使用非默认代码页,则必须始终使自己处于全局流状态.

As std::cout and std::wcout have static storage duration, they are guaranteed to be initialized before main is called, and consequently will always have the "C" locale at application startup; i.e., there is no point early enough in execution that one can call locale::global and change the default locale for std::cout and std::wcout. Thus, you must always imbue the global streams yourself if you want to use a non-default code page.

这篇关于wcout不将宽字符写出到命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆