ifstream或ofstream读取二进制文本乱码问题
2013-08-20 13:27
2807 查看
一:中文文件名乱码问题:
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读取二进制文本乱码问题
- 关于ofstream乱码的问题及其强制类型转化
- ifstream读取文件失败和乱码问题
- ifstream读取文件失败和乱码问题
- ifstream读取文件第一个字符乱码问题
- 输出数据到文本ofstream和从文本中读取数据ifstream
- .net读取xml文件中文乱码问题解决
- Eclipse读取xml中文乱码问题解决
- java中读取excel文件中字符串乱码问题解决方法
- Java读取properties文件中文乱码问题解决
- Flash读取XML文件的中文时出现乱码问题解决方法
- 关于java读取文本文件乱码的问题
- 解决MediaPlayer问题: [MP3Extractor: Unable to resync. Signalling end of stream]
- 关于file_get_contents读取http乱码问题
- ASP读取数据库的中文出现乱码问题
- mac上mysql数据库读取数据中文乱码问题
- Ubuntu 16.04 Python读取MySQL数据库中文乱码的问题
- properties 读取中文乱码问题
- QT5读取中文乱码问题