C/C++中读取中文路径文件的方法
2011-11-03 10:14
2903 查看
转自 http://apps.hi.baidu.com/share/detail/33007444
有时候用ifstream或ofstream打开带有中文路径的文件会失败。
解决办法:
1、使用C语言的函数设置为中文运行环境
setlocale(LC_ALL,"Chinese-simplified");
2、使用STL函数设置为系统语言环境
std::locale::global(std::locale(""));
当然选2啦!
ofstream writefile;
string filename=("d:\我的文档\测试.txt");
locale loc = locale::global(locale(""));
//要打开的文件路径含中文,设置全局locale为本地环境
writefile.open(filename.c_str(),ios::out);
//打开文件
locale::global(loc);//恢复全局locale
用locale对象的name方法可以看到,通过locale("")构造出的locale 对象的name为"Chinese_People's Republic of China.936",而原始的locale对象的name为"C",也就是缺省的ANSI_C公约。
注意:如果使用locale loc = locale::global(locale(""))设置全局locale后没有用 locale::global(loc)恢复的话,那么在程序后面的cout语句就不能输出中文了,虽然这时候操作中文文件没有问题,但是这也是很容易让 人掉入陷阱的地方,应该值得注意。解决方法:
先将CString 转为char*
CString str=view->m_sFilePath;
//CString str = "";
//ofstream 中传的参数不能有汉字,必须转换,一下是其暂缓方法
str.AppendFormat("cluster%d%d.txt",aMatrix->X_part,aMatrix->Y_part);
char* sz = str.GetBuffer(str.GetLength());
locale loc = locale::global(locale(""));
ofstream ofile(sz);
转载网友方法:
/* 方法1,使用_TEXT()宏定义将字符串常量指定为TCHAR*类型 这种情况下必须是unicode下编译 */
18: /* 如果是我,首选此类型 */
19: /************************************************************************/
20: fstream file;
21: file.open(_TEXT("c:\\测试\\测试文本.txt"));
22: cout<<file.rdbuf();
23: file.close();
24:
25: /************************************************************************/
26: /* 方法2,使用STL中的locale类的静态方法指定全局locale */
27: /* 使用该方法以后,cout可能不能正常输出中文,十分蹊跷 */
28: /* 我发现了勉强解决的方法:不要在还原区域设定前用cout或wcout 输出中文 */
29: /* 否则后果就是还原区域设定后无法使用cout wcout输出中文 */
30: /************************************************************************/
31: locale::global(locale(""));//将全局区域设为操作系统默认区域
32: file.open("c:\\测试\\测试文本2.txt");//可以顺利打开文件了
33: locale::global(locale("C"));//还原全局区域设定
34: cout<<file.rdbuf();
35: file.close();
36:
37: /************************************************************************/
38: /* 方法3,使用C函数setlocale,不能用cout输出中文的问题解决方法同上 */
39: /************************************************************************/
40: setlocale(LC_ALL,"Chinese-simplified");//设置中文环境
41: file.open("c:\\测试\\测试文本3.txt");//可以顺利打开文件了
42: setlocale(LC_ALL,"C");//还原
43: cout<<file.rdbuf();
44: file.close();
45: }
有时候用ifstream或ofstream打开带有中文路径的文件会失败。
解决办法:
1、使用C语言的函数设置为中文运行环境
setlocale(LC_ALL,"Chinese-simplified");
2、使用STL函数设置为系统语言环境
std::locale::global(std::locale(""));
当然选2啦!
ofstream writefile;
string filename=("d:\我的文档\测试.txt");
locale loc = locale::global(locale(""));
//要打开的文件路径含中文,设置全局locale为本地环境
writefile.open(filename.c_str(),ios::out);
//打开文件
locale::global(loc);//恢复全局locale
用locale对象的name方法可以看到,通过locale("")构造出的locale 对象的name为"Chinese_People's Republic of China.936",而原始的locale对象的name为"C",也就是缺省的ANSI_C公约。
注意:如果使用locale loc = locale::global(locale(""))设置全局locale后没有用 locale::global(loc)恢复的话,那么在程序后面的cout语句就不能输出中文了,虽然这时候操作中文文件没有问题,但是这也是很容易让 人掉入陷阱的地方,应该值得注意。解决方法:
先将CString 转为char*
CString str=view->m_sFilePath;
//CString str = "";
//ofstream 中传的参数不能有汉字,必须转换,一下是其暂缓方法
str.AppendFormat("cluster%d%d.txt",aMatrix->X_part,aMatrix->Y_part);
char* sz = str.GetBuffer(str.GetLength());
locale loc = locale::global(locale(""));
ofstream ofile(sz);
转载网友方法:
/* 方法1,使用_TEXT()宏定义将字符串常量指定为TCHAR*类型 这种情况下必须是unicode下编译 */
18: /* 如果是我,首选此类型 */
19: /************************************************************************/
20: fstream file;
21: file.open(_TEXT("c:\\测试\\测试文本.txt"));
22: cout<<file.rdbuf();
23: file.close();
24:
25: /************************************************************************/
26: /* 方法2,使用STL中的locale类的静态方法指定全局locale */
27: /* 使用该方法以后,cout可能不能正常输出中文,十分蹊跷 */
28: /* 我发现了勉强解决的方法:不要在还原区域设定前用cout或wcout 输出中文 */
29: /* 否则后果就是还原区域设定后无法使用cout wcout输出中文 */
30: /************************************************************************/
31: locale::global(locale(""));//将全局区域设为操作系统默认区域
32: file.open("c:\\测试\\测试文本2.txt");//可以顺利打开文件了
33: locale::global(locale("C"));//还原全局区域设定
34: cout<<file.rdbuf();
35: file.close();
36:
37: /************************************************************************/
38: /* 方法3,使用C函数setlocale,不能用cout输出中文的问题解决方法同上 */
39: /************************************************************************/
40: setlocale(LC_ALL,"Chinese-simplified");//设置中文环境
41: file.open("c:\\测试\\测试文本3.txt");//可以顺利打开文件了
42: setlocale(LC_ALL,"C");//还原
43: cout<<file.rdbuf();
44: file.close();
45: }
相关文章推荐
- C/C++中读取中文路径文件的方法
- 利用Pandas读取文件路径或文件名称包含中文的csv文件方法
- c++怎么读取文件中的中文字符串的几种方法
- java 读取文件路径空格、"+"和中文的处理方法
- C/C++ 向mysql数据库读取、写入中文的解决方法
- Pandas读取路径或文件名称包含中文的文件时报错及解决办法
- C++读取歌词文件(LRC),分解时间和歌词的方法
- Python文件读取的3种方法及路径转义
- java 使用相对路径读取文件File(类构造方法)
- C++ 利用string读取txt文件的方法
- java 读取文件路径中空格和中文的处理
- Java读取properties配置文件时,中文乱码解决方法
- 【IntelliJ IDEA】从资源文件读取出来就中文乱码的解决方法
- C++实现读取特定路径下文件夹及文件名的方法
- Java 读取和删除目录路径下所有文件的方法
- C++从txt文件中读取二维的数组方法
- Qt5处理绝对路径中含中文的文件方法
- Android读取中文文件乱码解决方法
- 解决OpenCV的imread函数无法读取中文路径和中文命名的文件的问题。
- C/C++ 读取16进制文件的方法