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

[C++]fstream与中文路径的问题

2012-12-21 14:05 106 查看
一、开始

用C++的fstream打开中文路径或者中文文件名的文件时,可能会有乱码或者打不开的问题。

 

ifstream is(strFileName);

if(!is.is_open())

{

   return0;

}

 

这里返回的是0,一般的原因都是路径的问题。

二、方法

//设置为系统缺省,并将原来的locale返回保存在loc中
 std::locale loc
= std::locale::global(std::locale(""));
 ifstream ifs(strFileName);
 if(!ifs.is_open())
 {
   return0;
 }
 ifs.close();
 
 //设置回原来的编码,默认情况下程序开始的locale
为"C"
 std::locale::global(loc);
 return1;

 
三、参考

http://new.cplusplus.com/reference/std/locale/locale/

http://social.msdn.microsoft.com/Forums/zh-CN/vcgeneral/thread/0fb287af-bb58-4c60-a3da-14fe84c16948

http://www.cppblog.com/deane/archive/2011/08/11/153007.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: