ofstream打开中文路径失败解决方法
来源:互联网 发布:新顶级域名不备案吗 编辑:程序博客网 时间:2024/06/06 22:41
用ofstream打开中文路径名会失败问题,解决方法为:std::locale loc = std::locale::global(std::locale(""));
std::ofstream fout(FilePath);//do some work herefout.close();
std::locale::global( loc );
用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语句就不能输出中文了。
转载网友方法:
/* 方法1,使用_TEXT()宏定义将字符串常量指定为TCHAR*类型 这种情况下必须是unicode下编译 */ /* 如果是我,首选此类型 */ /************************************************************************/ fstream file; file.open(_TEXT("c:\\测试\\测试文本.txt")); cout<<file.rdbuf(); file.close(); /************************************************************************/ /* 方法2,使用STL中的locale类的静态方法指定全局locale */ /* 使用该方法以后,cout可能不能正常输出中文,十分蹊跷 */ /* 我发现了勉强解决的方法:不要在还原区域设定前用cout或wcout 输出中文 */ /* 否则后果就是还原区域设定后无法使用cout wcout输出中文 */ /************************************************************************/ locale::global(locale(""));//将全局区域设为操作系统默认区域 file.open("c:\\测试\\测试文本2.txt");//可以顺利打开文件了 locale::global(locale("C"));//还原全局区域设定 cout<<file.rdbuf(); file.close(); /************************************************************************/ /* 方法3,使用C函数setlocale,不能用cout输出中文的问题解决方法同上 */ /************************************************************************/ setlocale(LC_ALL,"Chinese-simplified");//设置中文环境 file.open("c:\\测试\\测试文本3.txt");//可以顺利打开文件了 setlocale(LC_ALL,"C");//还原 cout<<file.rdbuf(); file.close();
0 0
- ofstream打开中文路径失败解决方法
- ifstream(ofstream) 打开中文路径失败
- ifstream或ofstream打开带有中文路径的文件失败解决方法
- ifstream & ofstream 打开中文路径失败的解决方案
- 有时候用ifstream或ofstream打开带有中文路径的文件会失败。 解决办法
- ofstream打开中文路径的三种方法
- 关于std::fstream以及std::ifstream打开中文路径名失败的问题和解决方法
- vs2008 std::fstream以及std::ifstream打开中文路径名失败的解决方法
- 关于std::fstream以及std::ifstream打开中文路径名失败的问题和解决方法
- 关于std::fstream以及std::ifstream打开中文路径名失败的问题和解决方法
- ifstream VS2005 中文路径打开失败问题
- qt5 打开中文路径的图片失败
- qt5 打开中文路径的图片失败
- ifstream打开含中文路径文件时失败的问题
- ifstream打开含中文路径文件时失败的问题
- ifstream打开含中文路径名文件失败的原因
- sqlite打开路径含中文的数据库文件失败的问题
- ffmpeg中avio_open无法打开中文路径的解决方法
- 数据结构赫夫曼树源程序
- Android studio 下载安装
- 按键精灵微信群内加好友
- 使用qmeu-img创建虚拟机(创建虚拟机,虚拟机快照)
- Dalvik 虚拟机指令
- ofstream打开中文路径失败解决方法
- J2EE--运用JODConverter进行doc、ppt到pdf的转换
- I/O流
- 数据结构进制转换源代码
- json 数据类型
- Android studio 自动排版
- css基础学习之--媒体查询
- phpstorm下配置Xdebug
- 使用 jsoup 对 HTML 文档进行解析和操作