如何在 C++ 中编写西里尔文字安慰
例如,如果我写:
cout << "Привет!" << endl; //it's hello in Russian
在控制台中,它将类似于 ╧ЁштхЄ!
。
好的,我知道我们可以使用:
setlocale(LC_ALL, "Russian");
但在那之后,俄语的命令行参数不起作用(如果我通过 BAT 文件启动程序):
StartProgram.bat
chcp 1251
MyProgram.exe -user=Олег -password=Пароль
所以,在 setlocale 之后
程序无法正确读取俄语参数。
出现这种情况是因为BAT文件在CP1251中,但控制台在CP866中。
所以,有一个问题:
如何在 C++ 控制台中编写俄语文本,同时正确读取俄语命令行参数。
For example, if I write:
cout << "Привет!" << endl; //it's hello in Russian
In the console it would be something like ╧ЁштхЄ!
.
OK, I know that we can use:
setlocale(LC_ALL, "Russian");
But after that, command line arguments in Russian do not work (if I start my program through a BAT file):
StartProgram.bat
chcp 1251
MyProgram.exe -user=Олег -password=Пароль
So, after setlocale
the program can't read Russian arguments properly.
This happens because the BAT file in CP1251, but the console is in CP866.
So, there is a question:
How can I write Russian text in the C++ console and at the same time have Russian command line arguments read properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
请参阅 Michael Kaplan 博客中的此条目:
http://www.siao2.com/2008 /03/18/8306597.aspx
See this entry from Michael Kaplan's blog:
http://www.siao2.com/2008/03/18/8306597.aspx
您是否尝试过使用
wcout
?它类似于cout
,但它接受“宽”字符,这应该允许正确的 unicode 编码。这篇有关本地化的文章和另一个,都来自MSDN,可能有用。
Have you tried using
wcout
? It is similar tocout
, but it accepts "wide" characters, which should permit the proper unicode encodings.This article about localization, and another, both from MSDN may be of use.
控制台设置为 1251 而不是 866:
程序是 argument.exe,结果:
D:\Debug>argument Олег Пароль
你好 Олег Пароль
Console set to be in 1251 instead of in 866:
Program is argument.exe and result:
D:\Debug>argument Олег Пароль
Hello Олег Пароль
您可以尝试使用以下函数
setlocale()
和SetConsoleOutputCP( )
You can try using the following functions
setlocale()
andSetConsoleOutputCP()
对我来说,这似乎可以解决问题:
For me this seems to resolve the problem:
您是否在控制面板的区域和语言选项部分将非 unicode 程序的语言设置为俄语?
(我不知道俄语程序员的通常设置是什么;我只是想知道将其设置为某种英语以避免混淆过于狭隘的工具是否很常见。)
除非我的记忆在开玩笑,否则当我正在使用日本开发人员的一些代码,正是这一步使控制台正确显示非 Unicode 日语文本(Shift-JIS 编码)。
Have you set the language for non-unicode programs to be Russian, in the Regional and Language Options section of the Control Panel?
(I have no idea what the usual setup for Russian-speaking programmers might be; I just wonder whether it is common to set this to some kind of English to avoid confusing overly-parochial tools.)
Unless my memory is playing tricks, when I was working with some code from Japanese developers it was this step that got the console displaying non-Unicode Japanese text (Shift-JIS encoding) properly.
WriteConsoleW 可以毫无问题地处理 UNICODE,例如西里尔字母。如果您不想错过 wcout 的格式化功能,您可以重定向标准 wcout 流缓冲区并使用 WriteConsoleW 打印它。
此处显示了完整示例
WriteConsoleW can handle UNICODE e.g. Cyrillic letters without problems. If you won’t miss the formatting features of wcout, you can redirect the standard wcout stream buffer and print it with WriteConsoleW.
A full example is shown here
最正确的方法是使用 wcout + std::imbue。
但应该知道,Windows Vista/7 中的 setlocale API 发生了一些变化。 “Russian”区域设置字符串不再被识别为“cp866”,至少在 Visual C++ CRT 中是这样。
要获得 cp866 输出,请尝试使用以下命令:
The most correct way is using wcout + std::imbue.
But one should know that there was some changes in setlocale API which happened in Windows Vista/7. "Russian" locale string isn't recognized as "cp866" anymore, at least in Visual C++ CRT.
To get cp866 output, try use this instead: