ifstream或ofstream读取二进制文本乱码问题
2008-04-07 16:18
501 查看
一:中文文件名乱码问题:
1、使用C语言的函数设置为中文运行环境
setlocale(LC_ALL,"");
2、使用STL函数设置为系统语言环境
std::locale::global(std::locale(""));
// open file
std::locale::global(std::locale());
二:整型或者浮点型问题(比如“1000”,输出之后就成了“1,000”)
设置输出流的local为"C"即可,利用imbue()方法。
若输出流对象为a,则
a.imbue(std::locale("C"));
示例:
#include <fstream>
using namespace std;
bool Process(const char* dest,const char* src,LONG Offset,LONG Length)
{
locale::global(locale(""));
ifstream fin(src,ios::binary);
if(!fin)
{
return false;
}
fin.seekg(Offset);
ofstream fout(dest,ios::binary);
char* pbuf;
if (Length>0)
{
pbuf = new char[Length];
}
fin.imbue(locale("C"));
fin.read(pbuf,Length);
fout.write(pbuf,fin.gcount());
if (Length>0)
{
delete []pbuf;
}
fin.close();
fout.close();
locale::global(locale());
return true;
}
1、使用C语言的函数设置为中文运行环境
setlocale(LC_ALL,"");
2、使用STL函数设置为系统语言环境
std::locale::global(std::locale(""));
// open file
std::locale::global(std::locale());
二:整型或者浮点型问题(比如“1000”,输出之后就成了“1,000”)
设置输出流的local为"C"即可,利用imbue()方法。
若输出流对象为a,则
a.imbue(std::locale("C"));
示例:
#include <fstream>
using namespace std;
bool Process(const char* dest,const char* src,LONG Offset,LONG Length)
{
locale::global(locale(""));
ifstream fin(src,ios::binary);
if(!fin)
{
return false;
}
fin.seekg(Offset);
ofstream fout(dest,ios::binary);
char* pbuf;
if (Length>0)
{
pbuf = new char[Length];
}
fin.imbue(locale("C"));
fin.read(pbuf,Length);
fout.write(pbuf,fin.gcount());
if (Length>0)
{
delete []pbuf;
}
fin.close();
fout.close();
locale::global(locale());
return true;
}
相关文章推荐
- ifstream或ofstream读取二进制文本乱码问题
- ifstream或ofstream读取二进制文本乱码问题
- ifstream读取文件失败和乱码问题
- ifstream读取文件失败和乱码问题
- ifstream,fstream 读写文件问题,read读取字节不够
- ifstream连续读取文本文件时只能读取一个文件 其他文件读取失败问题
- VC与DEV编译器关于ifstream的文本反复读取问题存在的不同
- 转自【北漂民工】C++文件读取操作的全部函数讲解fstream(ofstream/ifstream)
- ifstream打印汉字乱码问题 UTF-8转GB2312
- C++读取写入.txt文件(ifstream/ofstream)—读取指定行,修改指定行,复制文件,清除文件,统计文件行数
- C++读取写入.txt文件(ifstream/ofstream)—读取指定行,修改指定行,复制文件,清除文件,统计文件行数
- VC中ifstream中文路径读取失败的问题
- C++ 读取配置文件的 ofstream和ifstream详细用法
- C++中string类及文件流类(ofstream,ifstream)的基本操作---按行读取文档
- ifstream 文件流读取格式数据出现的问题
- 关于ofstream乱码的问题及其强制类型转化
- C++文件读写详解(ofstream,ifstream,fstream)
- 解决jsp页面读取XXX.properties文件显示文乱码的问题
- ifstream与ofstream (下)
- JAVA读取TXT文档乱码问题