简体   繁体   English

wprintf不打印特殊字符

[英]wprintf not printing special characters

I'm trying to print the argument used to execute the program with the following code: 我正在尝试使用以下代码打印用于执行程序的参数:

#include "stdafx.h"
#include<stdio.h>
#include<locale>

int main(int argc, wchar_t* argv[])
{
    std::locale::global(std::locale(""));
    wprintf(L"Parameter sent: %s", argv[1]);
    return 0;
}

However, if I using the following command to execute the program: 但是,如果我使用以下命令来执行该程序:

AppName.exe SomeText-éãö

It prints: 它打印:

Parameter sent: ?????????????

According to Microsoft , multibyte-character environment is created by default if main is used. 根据Microsoft的说法,如果使用main ,默认情况下会创建多字节字符环境。 A wide-character environment is created by default if wmain is used. 如果使用wmain则默认情况下会创建一个宽字符环境。

Thus, it turns out that there are two ways to solve this problem. 因此,事实证明有两种方法可以解决这个问题。 I'll use Chinese characters as an example, by setting Command Arguments to "中国" (meaning "China") in Debugging tab of project's Property Pages . 我将使用中文字符作为示例,通过在项目属性页的 调试选项卡中将命令参数设置为“中国”(意思是“中国”)。

Basically, you need set the code page used by the console input and output, in addition to the language locale. 基本上,除了语言区域设置之外,还需要设置控制台输入和输出使用的代码页。 Otherwise, the console may not display your characters correctly. 否则,控制台可能无法正确显示您的字符。 That's why you see "??????.." because the characters just don't exist in the code page the console currently uses. 这就是你看“?????? ..”的原因,因为控制台当前使用的代码页中不存在这些字符。

1.Use multi-byte character set. 1.使用多字节字符集。

// Since we use MBCS, narrow-character version main should be used
int main(int argc, char *argv[])
{
   // You console may not use the code page you want.
   printf ("%d\n", GetConsoleCP());
   printf ("%d\n", GetConsoleOutputCP());

   // To read/write Chinese characters, we set code page to 936.
   SetConsoleCP(936);
   SetConsoleOutputCP(936);

   // Set C++ locale. Same to "Chinese_China.936" here.
   locale old = locale::global(locale(""));
   locale cur;
   printf ("old locale: %s\n", old.c_str());
   printf ("new locale: %s\n", cur.c_str());

   printf("Parameter sent: %s\n", argv[1]);
   getchar();
}

which outputs, 哪个输出,

1252
1252
old locale: C
new locale: Chinese (Simplified)_People's Republic of China.936
Parameter sent: 中国

2.Use Unicode. 2.使用Unicode。

// Since we use Unicode, wide-character version wmain should be used
int wmain(int argc, wchar_t* argv[])
{
   // You console may not use the code page you want.
   printf ("%d\n", GetConsoleCP());
   printf ("%d\n", GetConsoleOutputCP());

   // To read/write Chinese characters, we set code page to 936.
   SetConsoleCP(936);
   SetConsoleOutputCP(936);

   // Set C++ locale. Same to "Chinese_China.936" here.
   locale old = locale::global(locale(""));
   locale cur;
   printf ("old locale: %s\n", old.c_str());
   printf ("new locale: %s\n", cur.c_str());

   wprintf(L"Parameter sent: %ls\n", argv[1]);
   return 0;
}

which outputs, 哪个输出,

1252
1252
old locale: C
new locale: Chinese (Simplified)_People's Republic of China.936
Parameter sent: 中国

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM