Mythma‘s C++ blog
用5年时间来学C++.
posts - 78,comments - 372,trackbacks - 0

 

问题:
有时候用ifstream或ofstream打开带有中文路径的文件会失败。

解决办法:
1、使用C语言的函数设置为中文运行环境
setlocale(LC_ALL,"Chinese-simplified");

2、使用STL函数设置为系统语言环境
std::locale::global(std::locale(""));

当然选2啦!

posted on 2006-06-09 22:01 力为 阅读(3777) 评论(14)  编辑 收藏 引用 所属分类: C++ FAQGP&STL

FeedBack:
# re: fstream 和 中文路径
2006-06-09 22:21 | LOGOS
听君一席话,胜读十年书。
以前用VS2003的时候,用fstream类一直可以很好支持中文文件名的,后来换了VS2005之后,建立中文文件名的文件,文件名字却一直是乱码(wfstream不在此例),内心十分疑惑。
看了你的贴,做了一下测试,所有问题都OK了。  回复  更多评论
  
# re: fstream 和 中文路径
2006-06-09 23:25 | LOGOS
做了更多的测试之后,发现ios::imbue方法失效了,要改变语言类别,只能通过locale::global实现。
但是问题却来了,设置global为中文后,cout却丧失了输出中文的能力。。。  回复  更多评论
  
# re: fstream 和 中文路径
2006-06-10 11:13 | 力为
VC6可以正常输出,
在VC2005下cout不能正常输出,但ofstream可以正常输出。  回复  更多评论
  
# re: fstream 和 中文路径
2006-07-29 23:02 | smilerain
就是这个麻烦的问题
fstream能找到中文路径了
cout确不能输出中文了.  回复  更多评论
  
# re: fstream 和 中文路径
2006-08-27 20:38 | wuqiu
std::locale::global(std::locale(""));
// open file
std::locale::global(std::locale(0));
  回复  更多评论
  
# re: fstream 和 中文路径
2006-11-08 18:58 | Taorama
升级到VS2005,发现很多东西不能用了。跟踪半天,发现是fstream搞得鬼。试了很多办法都无效。
老大提供的方法不错,马上就搞定了。STL学的不好,老大是能解释一下吗。  回复  更多评论
  
# re: fstream 和 中文路径
2006-11-08 20:10 | Taorama
“1、使用C语言的函数设置为中文运行环境
setlocale(LC_ALL,"Chinese-simplified");”

这句话应该改为
setlocale(LC_ALL,"");
才对。  回复  更多评论
  
# re: fstream 和 中文路径
2006-11-14 12:02 | doermlp
请教楼主,用了上面的方法2之后的确能解决中文路径问题,但是向文件中写入整型或浮点型数据时有问题,比如“1000”,输出之后就成了“1,000”,这个问题能解决吗?谢谢。  回复  更多评论
  
# re: fstream 和 中文路径
2007-07-04 02:19 | 天津大学计算机学院 常兴龙
很不错!  回复  更多评论
  
# re: fstream 和 中文路径
2007-08-23 11:37 | 小胡
比如“1000”,输出之后就成了“1,000”,这个问题我也遇到了,文件名也会这样  回复  更多评论
  
# re: fstream 和 中文路径
2007-08-23 11:38 | 小胡
@小胡
有解决的办法码
  回复  更多评论
  
# re: fstream 和 中文路径
2007-09-15 02:05 | lax
@小胡
设置输出流的local为"C"即可,利用imbue()方法。
若输出流对象为a,则
a.imbue(std::locale("C"));
这样就不会出现1,000的现象。
  回复  更多评论
  
# re: fstream 和 中文路径
2007-12-30 08:51 | sdgsdg
http://foto-lingerie-ragazza.gradis-tun.info
http://sesso-ragazza-giovane.fatte-tun.info
http://scopata-gay-negro.sculaccia-tun.info
http://sexy-chiappa.sculaccia-tun.info
http://marito-guardone-moglie-troia.sculaccia-tun.info
http://sexy-toy-coppia.fatte-tun.info
http://manga-xxx-xxx-xxx-gratis.sculaccia-tun.info
http://coppia-escort-vicenza.gradis-tun.info
http://doctor-fetish-video.fatte-tun.info
http://donna-sotto-doccia-sesso-video-gratis.fatte-tun.info
http://sexi-shop-de.gradis-tun.info
http://sesso-prima-volta-it.gradis-tun.info
http://gay-foto-gratis.gradis-tun.info
http://anziana-molto-sex.gradis-tun.info
http://film-italiani-gratuiti.fatte-tun.info
http://ag-immobiliare-passera-giampiero-it.fatte-tun.info
http://foto-racconto-erotico-gratis.gradis-tun.info
http://racconto-porno-mamma-figlio.fatte-tun.info
http://diciottenne-bella-coscia.sculaccia-tun.info
http://foto-ragazza-nuda-pelli-vagina.gradis-tun.info
http://annuncio-ragazza-eper-scambio-coppia.fatte-tun.info
http://xxx-animale-hard.sculaccia-tun.info
http://mature-in-cerca.gradis-tun.info
http://confessioni-erotiche-di-mamme.fatte-tun.info
http://video-gratis-donna-matura.sculaccia-tun.info
http://intasare-passera.sculaccia-tun.info
http://video-porno-del-grande-fratello.gradis-tun.info
http://foto-naruto-porno.fatte-tun.info
http://pene-erezione-immagine.fatte-tun.info
http://foto-mutande.fatte-tun.info
http://diventeranno-famosa-porno.fatte-tun.info
http://videoz-xxx-com.sculaccia-tun.info
http://matura-taboo-001.gradis-tun.info
http://racconto-incestuosi-a69.gradis-tun.info
http://figa-aperta-anteprima-gratis.sculaccia-tun.info
http://libero-it-gay-sadomaso.gradis-tun.info
http://porno-guardone-video-gratis.gradis-tun.info  回复  更多评论
  
# re: fstream 和 中文路径
2008-01-02 19:14 | 进而人员
谢谢!  回复  更多评论
  

标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]
相关链接:
网站导航: