1: /********************************************************************
2: created: 2008/05/10
3: created: 10:5:2008 23:56
4: filename: k:\sj\fstreamTest\fstreamTest\main.cpp
5: file path: k:\sj\fstreamTest\fstreamTest
6: file base: main
7: file ext: cpp
8: author: Gohan
9: *********************************************************************/
10: #include <tchar.h>
11: #include <fstream>
12: #include <iostream>
13: using namespace std;
14: int main()
15: {
16: /************************************************************************/
17: /* 方法1,使用_TEXT()宏定義將字符串常量指定為TCHAR*類型 */
18: /* 如果是我,首選此類型 */
19: /************************************************************************/
20: fstream file;
21: file.open(_TEXT("c:\\測試\\測試文本.txt"));
22: cout<<file.rdbuf();
23: file.close();
24:
25: /************************************************************************/
26: /* 方法2,使用STL中的locale類的靜態(tài)方法指定全局locale */
27: /* 使用該方法以后,cout可能不能正常輸出中文,十分蹊蹺 */
28: /* 我發(fā)現(xiàn)了勉強(qiáng)解決的方法:不要在還原區(qū)域設(shè)定前用cout或wcout 輸出中文 */
29: /* 否則后果就是還原區(qū)域設(shè)定后無法使用cout wcout輸出中文 */
30: /************************************************************************/
31: locale::global(locale(""));//將全局區(qū)域設(shè)為操作系統(tǒng)默認(rèn)區(qū)域
32: file.open("c:\\測試\\測試文本2.txt");//可以順利打開文件了
33: locale::global(locale("C"));//還原全局區(qū)域設(shè)定
34: cout<<file.rdbuf();
35: file.close();
36:
37: /************************************************************************/
38: /* 方法3,使用C函數(shù)setlocale,不能用cout輸出中文的問題解決方法同上 */
39: /************************************************************************/
40: setlocale(LC_ALL,"Chinese-simplified");//設(shè)置中文環(huán)境
41: file.open("c:\\測試\\測試文本3.txt");//可以順利打開文件了
42: setlocale(LC_ALL,"C");//還原
43: cout<<file.rdbuf();
44: file.close();
45: }