ofstream打开中文路径的三种方法
来源:互联网 发布:意林小小姐阅读软件 编辑:程序博客网 时间:2024/06/03 14:54
经测试第一种方法在VS2008环境下无效
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类的静态方法指定全局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();
- ofstream打开中文路径的三种方法
- ifstream & ofstream 打开中文路径失败的解决方案
- ofstream打开中文路径失败解决方法
- ifstream(ofstream) 打开中文路径失败
- 有时候用ifstream或ofstream打开带有中文路径的文件会失败。 解决办法
- ifstream或ofstream打开带有中文路径的文件失败解决方法
- Qt利用QProcess打开路径(含中文路径)的方法
- Vs2008中ofstream不能读取带有中文的路径
- 解决std::ofstream操作中文路径下文件的问题
- ifstream打开中文路径的问题
- ifstream打开中文路径的问题
- luacom打开中文路径的Word文件
- GDAL GDALOpen 打开中文路径的问题
- python打开中文路径的应用程序
- qt5 打开中文路径的图片失败
- MFC 打开有中文的路径
- qt5 打开中文路径的图片失败
- 打开对方CMD的三种方法!
- JGroup 使用分享
- 用Delphi写一个Windows下的纯SDK程序
- Web.config详解
- requesthttp 创建和使用
- csdn,真好
- ofstream打开中文路径的三种方法
- 结合spring+hibernate与jdbc的事务,From 荣浩
- 继承的理解
- oralce一些用法
- JDBC对数据库的事务操作
- 实现透明水印
- 怎样防止浏览器刷新重复提交数据?
- SQL2005中大数据量检索的分页
- struts2-准备开发环境