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

来源:互联网 发布:itunes软件备份路径 编辑:程序博客网 时间:2024/06/05 17:02

首先,要保证你的控制台的当前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恢复为默认值。

0 0