如何在 C++ 中编写西里尔文字安慰

发布于 2024-08-21 16:09:31 字数 548 浏览 4 评论 0原文

例如,如果我写:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

想你的星星会说话 2024-08-28 16:09:31

请参阅 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

友谊不毕业 2024-08-28 16:09:31

您是否尝试过使用wcout?它类似于 cout,但它接受“宽”字符,这应该允许正确的 unicode 编码。

这篇有关本地化的文章另一个,都来自MSDN,可能有用。

Have you tried using wcout? It is similar to cout, 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.

羅雙樹 2024-08-28 16:09:31

控制台设置为 1251 而不是 866:

 //Save As Windows 1251
    #include<stdio.h>
    #include<windows.h>
    int main(int argc, char **argv){ 
        SetConsoleOutputCP(1251);
        SetConsoleCP(1251);
        if(argc<2)return 0;
        else printf("Hello %s %s\n",argv[1],argv[2]);
    } 

程序是 argument.exe,结果:

D:\Debug>argument Олег Пароль
你好 Олег Пароль

Console set to be in 1251 instead of in 866:

 //Save As Windows 1251
    #include<stdio.h>
    #include<windows.h>
    int main(int argc, char **argv){ 
        SetConsoleOutputCP(1251);
        SetConsoleCP(1251);
        if(argc<2)return 0;
        else printf("Hello %s %s\n",argv[1],argv[2]);
    } 

Program is argument.exe and result:

D:\Debug>argument Олег Пароль
Hello Олег Пароль

您可以尝试使用以下函数setlocale()SetConsoleOutputCP( )

setlocale(LC_ALL, "Russian");
SetConsoleOutputCP(866);

You can try using the following functions setlocale() and SetConsoleOutputCP()

setlocale(LC_ALL, "Russian");
SetConsoleOutputCP(866);
╭ゆ眷念 2024-08-28 16:09:31

对我来说,这似乎可以解决问题:

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

using namespace std;

int main(void) {
    _setmode(_fileno(stdout), _O_U16TEXT);
    wcout << L"Огњен" << endl;
    return 0;
}

For me this seems to resolve the problem:

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

using namespace std;

int main(void) {
    _setmode(_fileno(stdout), _O_U16TEXT);
    wcout << L"Огњен" << endl;
    return 0;
}
雨的味道风的声音 2024-08-28 16:09:31

您是否在控制面板的区域和语言选项部分将非 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.

只为守护你 2024-08-28 16:09:31

WriteConsoleW 可以毫无问题地处理 UNICODE,例如西里尔字母。如果您不想错过 wcout 的格式化功能,您可以重定向标准 wcout 流缓冲区并使用 WriteConsoleW 打印它。

此处显示了完整示例

// save and redirect cout buffer
wostringstream  newCoutBuffer;
wstreambuf*     oldCoutBuffer = wcout.rdbuf(newCoutBuffer.rdbuf());    

// do your wcout stuff here
// do your wcout stuff here

DWORD dwWritten;
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), newCoutBuffer.str().c_str(),newCoutBuffer.tellp(),&dwWritten,NULL);  

// restore cout buffer
wcout.rdbuf(oldCoutBuffer);

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

// save and redirect cout buffer
wostringstream  newCoutBuffer;
wstreambuf*     oldCoutBuffer = wcout.rdbuf(newCoutBuffer.rdbuf());    

// do your wcout stuff here
// do your wcout stuff here

DWORD dwWritten;
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), newCoutBuffer.str().c_str(),newCoutBuffer.tellp(),&dwWritten,NULL);  

// restore cout buffer
wcout.rdbuf(oldCoutBuffer);
情何以堪。 2024-08-28 16:09:31

最正确的方法是使用 wcout + std::imbue。

但应该知道,Windows Vista/7 中的 setlocale API 发生了一些变化。 “Russian”区域设置字符串不再被识别为“cp866”,至少在 Visual C++ CRT 中是这样。

要获得 cp866 输出,请尝试使用以下命令:

::setlocale( LC_ALL , "russian_russia.866" );

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:

::setlocale( LC_ALL , "russian_russia.866" );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文