如何在Windows上将拉丁字符正确打印到C ++控制台? [英] How to print Latin characters to the C++ console properly on Windows?

查看:77
本文介绍了如何在Windows上将拉丁字符正确打印到C ++控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用C ++将法语字符写入控制台时遇到问题.使用 std :: ifstream std :: getline 从文件中加载字符串,然后使用 std :: cout 将其打印到控制台.这是文件中的字符串:

I'm having a problem writing French characters to the console in C++. The string is loaded from a file using std::ifstream and std::getline and then printed to the console using std::cout. Here is what the string is in the file:

Lachaînequi对应的au代码为"TEST_CODE",语言环境为"fr".

La chaîne qui correspond au code "TEST_CODE" n'a pas été trouvée à l'aide locale "fr".

这是字符串的打印方式:

And here is how the string is being printed:

Lachaénequi对应的代码为"TEST_CODE",n'a pasÚtÚtrouvÚeÓl'aide语言环境为"fr".

La cha¯ne qui correspond au code "TEST_CODE" n'a pas ÚtÚ trouvÚe Ó l'aide locale "fr".

如何解决此问题?

推荐答案

问题是控制台使用的代码页与系统其余部分不同.例如,通常为美洲和西欧设置的Windows系统使用CP1252,但是这些地区的控制台使用CP437或CP850.

The issue is that the console uses different code pages than the rest of the system. For example normally Windows systems set up for the Americas and Western Europe use CP1252, but the console in those regions uses CP437 or CP850.

您可以将控制台输出代码页设置为与您使用的编码相匹配,也可以将字符串转换为与控制台输出代码页相匹配的

You can either set the console output code page to match the encoding you're using or you can convert the strings to match the console's output code page.

设置控制台输出代码页:

Set the console output codepage:

SetConsoleOutputCP(GetACP()); // GetACP() returns the system codepage.
std::cout << "La chaîne qui correspond au code \"TEST_CODE\" n'a pas été trouvée à l'aide locale \"fr\".";

或者是在编码之间进行转换的多种方法之一(此方法需要VS2010或更高版本):

Or one of many ways to convert between encodings (this one requires VS2010 or greater):

#include <codecvt> // for wstring_convert
#include <locale>  // for codecvt_byname
#include <iostream>

int main() {
    typedef std::codecvt_byname<wchar_t,char,std::mbstate_t> codecvt;

    // the following relies on non-standard behavior, codecvt destructors are supposed to be protected and unusable here, but VC++ doesn't complain.
    std::wstring_convert<codecvt> cp1252(new codecvt(".1252"));
    std::wstring_convert<codecvt> cp850(new codecvt(".850"));

    std::cout << cp850.to_bytes(cp1252.from_bytes("...été trouvée à...\n")).c_str();
}

后一个示例假定您确实需要在1252和850之间进行转换.您可能应该使用GetOEMCP()函数来找出实际的目标代码页,而源代码页实际上取决于您对源代码使用的内容.代码而不是运行程序的计算机上的GetACP()结果.

The latter example assumes you do in fact need to convert between 1252 and 850. You should probably use the function GetOEMCP() to figure out the actual target code page, and the source codepage actually depends on what you use for the source code rather than on the result of GetACP() on the machine running the program.

还请注意,该程序依赖于标准未保证的内容:在区域设置之间共享wchar_t编码.在大多数平台上都是如此-通常在所有语言环境中对wchar_t使用某种Unicode编码,但并非全部.

Also note that this program relies on something not guaranteed by the standard: that the wchar_t encoding be shared between locales. This is true on most platforms—usually some Unicode encoding is used for wchar_t in all locales—but not all.

理想情况下,您可以在任何地方都使用UTF-8,下面的方法就可以正常工作,就像现在在其他平台上一样:

Ideally you could just use UTF-8 everywhere and the following would work fine, as it does on other platforms these days:

#include <iostream>

int main() {
    std::cout << "La chaîne qui correspond au code \"TEST_CODE\" n'a pas été trouvée à l'aide locale \"fr\".\n";
}

不幸的是,如果不放弃将UTF-16用作wchar_t编码并采用4字节wchar_t,或者违反标准要求并破坏符合标准的程序,Windows将无法以这种方式支持UTF-8.

Unfortunately Windows can't support UTF-8 this way without either abandoning UTF-16 as the wchar_t encoding and adopting a 4 byte wchar_t, or violating requirements of the standard and breaking standard conforming programs.

这篇关于如何在Windows上将拉丁字符正确打印到C ++控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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