fstream默认不支持中文路径和输出整数带逗号的解决办法
2014-01-17 14:57
309 查看
今天项目中打日志,发现带中文路径名就不能成功创建,经网上了解,发现c++的一套文件IO库,默认是不支持中文路径的。
下面我们用fstream来创建一个新文件,如果文件路径中带有中文,则创建一般会失败。如下面代码:
view
plain
#include <iostream>
#include <fstream>
#include <string>
#include <direct.h>
using namespace std;
void main()
{
_mkdir("测试"); //新建一个中文文件夹
ofstream outfile( "测试/test.txt", ios::out ); //创建文件
if( !outfile )
{
cout << "Failed to create file!";
return ;
}
outfile.close();
}
程序将输出创建文件夹失败的信息。
一个解决办法是:在中文操作系统下,调用locale::global(std::locale("")),将全局区域设置为中文,如下例:
view plain
#include <iostream>
#include <fstream>
#include <string>
#include <direct.h>
using namespace std;
void main()
{
locale::global(std::locale("")); //将全局区域设为操作系统默认区域,以支持中文路径
_mkdir("测试"); //新建一个中文文件夹
ofstream outfile( "测试/test.txt", ios::out ); //创建文件
if( !outfile )
{
cout << "Failed to create file!";
return ;
}
int i = 123456789;
outfile << "i = " << i << "/n"; //输出带逗号
outfile.close();
setlocale( LC_ALL, "C" ); //恢复全局locale,如果不恢复,可能导致cout无法输出中文
}
创建文件成功,在程序的“测试”文件下出现个test.txt文件。需要注意的是最后需要调用setlocale( LC_ALL, "C" )还原,否则会出现未知错误。
此时我们发现,test.txt中的i值显示为123,456,789,出现了逗号。这是因为中文习惯问题。我们在读取文件的时候,读入i值时,可能读入一个值为123的整数,并不是我们希望的123456789,所以我们写文件时,希望不要写入逗号。一个办法是现将整数转换为字符串再进行输出,如:
view plain
int i = 123456789;
char ch[20];
sprintf((char *)&ch, "%d", i); //整数数据转换为字符数组。
outfile << "i = " << ch << '/n'; //输出不带逗号
上述问题的解决方法有很多,大家可以试试。
下面我们用fstream来创建一个新文件,如果文件路径中带有中文,则创建一般会失败。如下面代码:
view
plain
#include <iostream>
#include <fstream>
#include <string>
#include <direct.h>
using namespace std;
void main()
{
_mkdir("测试"); //新建一个中文文件夹
ofstream outfile( "测试/test.txt", ios::out ); //创建文件
if( !outfile )
{
cout << "Failed to create file!";
return ;
}
outfile.close();
}
程序将输出创建文件夹失败的信息。
一个解决办法是:在中文操作系统下,调用locale::global(std::locale("")),将全局区域设置为中文,如下例:
view plain
#include <iostream>
#include <fstream>
#include <string>
#include <direct.h>
using namespace std;
void main()
{
locale::global(std::locale("")); //将全局区域设为操作系统默认区域,以支持中文路径
_mkdir("测试"); //新建一个中文文件夹
ofstream outfile( "测试/test.txt", ios::out ); //创建文件
if( !outfile )
{
cout << "Failed to create file!";
return ;
}
int i = 123456789;
outfile << "i = " << i << "/n"; //输出带逗号
outfile.close();
setlocale( LC_ALL, "C" ); //恢复全局locale,如果不恢复,可能导致cout无法输出中文
}
创建文件成功,在程序的“测试”文件下出现个test.txt文件。需要注意的是最后需要调用setlocale( LC_ALL, "C" )还原,否则会出现未知错误。
此时我们发现,test.txt中的i值显示为123,456,789,出现了逗号。这是因为中文习惯问题。我们在读取文件的时候,读入i值时,可能读入一个值为123的整数,并不是我们希望的123456789,所以我们写文件时,希望不要写入逗号。一个办法是现将整数转换为字符串再进行输出,如:
view plain
int i = 123456789;
char ch[20];
sprintf((char *)&ch, "%d", i); //整数数据转换为字符数组。
outfile << "i = " << ch << '/n'; //输出不带逗号
上述问题的解决方法有很多,大家可以试试。
相关文章推荐
- fstream默认不支持中文路径和输出整数带逗号的解决办法
- fstream默认不支持中文路径和输出整数带逗号的解决办法
- GitBash不支持中文(终端输出显示中文乱码)的简单解决办法(自测试Okay)
- Tomcat不支持中文路径的解决办法
- apache2不支持中文路径的解决办法
- Apache HTTP Server路径不支持中文字符问题的解决办法
- tomcat不支持中文路径(链接)的解决办法
- vc2008 fstream不支持中文路径的解决方案
- springmvc 资源映射默认不支持断点续传 断点下载的解决办法
- Tomcat无法访问中文路径的解决办法
- 有时候用ifstream或ofstream打开带有中文路径的文件会失败。 解决办法
- [转]utf-8编码引起js输出中文乱码的解决办法
- Tomcat服务器图片地址中文路径问题解决办法
- wprintf_s不能正常输出中文的解决办法
- AndroidStudio IDE主题不支持中文的解决办法
- Busybox支持中文的解决办法
- 【菜鸟向】输入三个整数对比大小后输出。和VC2008下提示找不到MSVCP90D.dll的解决办法。
- 在 Sublime Text 2 中编译和运行 Java 程序,以及输出中文出错问题解决办法
- vc2008 fstream不支持中文路径的解决方案
- vs2008 std::fstream以及std::ifstream打开中文路径名失败的解决方法