您的位置:首页 > 编程语言 > C语言/C++

C++ Tips: 在控制台中显示中文

2015-08-24 12:15 483 查看
首先,要保证你的控制台的当前code page是中文:



代码示例:

// CppAnalyzerConsole.cpp : Defines the entry point for the console application.
//

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

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
_TCHAR* inputString;

if (argc < 2)
{
cout << "Please input command line parameter." << endl;
cout << "Usage: CppAnalyzerConsole <InputString>" << endl;
goto EXIT;
}

inputString = argv[1];
setlocale(LC_ALL, "Chinese-simplified");
_tprintf(_TEXT("InputString: %s\n"), inputString);

EXIT:

setlocale(LC_ALL, "C");
return 0;
}


测试:



讲解:

控制台的默认locale设置是“C”,要想输出中文,就必须将locale设置成中文。

setlocale(LC_ALL, "Chinese-simplified");


这句命令就是将locale设置为简体中文,而

setlocale(LC_ALL, "C");


这句命令是将local恢复为默认值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ c语言