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

c++ 用setlocale 设置 cout 输出中文

2012-06-14 22:03 393 查看
char * setlocale ( int category, const char * locale );

Set or retrieve locale
Sets locale information to be used by the current program, either changing the entire locale or parts of it. The function can also be used to retrieve the current locale's name by passing NULL as the value for locale.

Locales contain information on how to interpret and perform certain input/output and transformation operations taking into consideration location and language specific settings.

Most running environments have certain locale information set according to the user preferences or localization. But, independently of this system locale, on start, all C programs have the "C" locale set, which is a rather neutral locale with minimal locale information that allows the result of programs to be predictable. In order to use the default locale set in the environment, this function can be called with "" as the locale parameter.

The locale set on start is the same as setlocale(LC_ALL,"C") would set.
The entire default locale can be set by calling setlocale(LC_ALL,"");
The parts of the current locale affected by a call to this function are specified by parameter category.

配置地域化信息。

本函数用来配置地域的信息,设置当前程序使用的本地化信息。参数 category 有下列的选择:

* LC_ALL 包括下面的全项选项都要。
* LC_COLLATE 配置字符串比较,PHP 目前尚未实作出来本项。
* LC_CTYPE 配置字符类别及转换。例如全变大写 strtoupper()。
* LC_MONETARY 配置金融货币,PHP 目前尚未实作。
* LC_NUMERIC 配置小数点后的位数。
* LC_TIME 配置时间日期格式,与 strftime() 合用。

而参数 locale 若是空字符串 "",则会使用系统环境变量的 locale 。若 locale 为零(NULL),则不会改变地域化配置,返回当前的地域值,若系统尚未实作则返回 false。

setlocale( LC_ALL, ".936" ); 设置中文

转:

VS2005中的中文编码问题两则

由于VS2005比VC6.0提供了更为复杂的Unicode和多字节字符集支持,因此在一些时候可能出现字符集不匹配的情况。

第一个问题是在使用C++中的ifsteam和ofstram打开含有中文的路径的时候无法正常打开。网上提供的解决方法大致有三种:

1. 将项目属性->字符集设置为使用Unicode字符集,并在所有字符串外面面加上_T() 或者_TEXT宏,代价是原来所有不符合unicode规范的地方都必须得改。

2. 不更改项目属性->字符集,并在程序初始化的时候调用C语言中的函数setlocale()设置中文运行环境如下:

setlocale(LC_ALL,"Chinese-simplified");

3. 不更改项目属性->字符集,使用STL函数设置系统语言环境

std::locale::global(std::locale(""));

最后一种方法是C++中比较常用的,但是后两种方法在打开文件后要将运行环境还原,否则在cout的时候就不能输出中文了。

/***********************************************************************/
2
/* 方法1,使用_TEXT()宏定义将字符串常量指定为TCHAR*类型,须是unicode下编译     */
3
/***********************************************************************/
4
fstream file;
5
file.open(_TEXT("c:\\测试\\测试文本.txt"));
6
cout<<file.rdbuf();
7
file.close();
01
/***************************************************************/
02
/* 方法2,使用C函数setlocale                                     */
03
/* 使用该方法以后,cout可能不能正常输出中文,十分蹊跷                 */
04
/* 我发现了勉强解决的方法:不要在还原区域设定前用cout或wcout 输出中文  */
05
/* 否则后果就是还原区域设定后无法使用cout wcout输出中文              */
06
/***************************************************************/
07
setlocale(LC_ALL,"Chinese-simplified");//设置中文环境
08
file.open("c:\\测试\\测试文本3.txt");//可以顺利打开文件了
09
setlocale(LC_ALL,"C");//还原
10
cout<<file.rdbuf();
11
file.close();
1
/****************************************************************/
2
/* 方法3,使用STL中的locale类的静态方法指定全局locale                */
3
/* 不能用cout输出中文的问题解决方法同上                              */
4
/****************************************************************/
5
locale::global(locale(""));//将全局区域设为操作系统默认区域
6
file.open("c:\\测试\\测试文本2.txt");//可以顺利打开文件了
7
locale::global(locale("C"));//还原全局区域设定
8
cout<<file.rdbuf();
9
file.close();


深入:

/article/4601993.html

windows 获取以及更改CMD控制台编码

http://www.360doc.com/content/12/0518/11/203871_211854106.shtml

/article/4933243.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: