c++ 用setlocale 設置 cout 輸出中文


 

   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();

 

 

深入:

http://www.cnblogs.com/hnrainll/archive/2011/05/07/2039700.html

 

windows 獲取以及更改CMD控制台編碼

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

http://www.cnblogs.com/moonz-wu/archive/2008/12/30/1365552.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM