ifStream读取ANSI/Unicode/Utf8文本保存到string里并转成gbk编码
2017-02-03 22:04
731 查看
ifstream读取unicode文本到string时,需要过滤文本开始ff fe两个字节,否则转成gbk会失败。
ifstream读取utf8文本到string时,需要过滤文本开始efbbbf三个字节,否则转成gbk会失败。
下面是实现代码:
#include <iostream>
#include <string>
#include <fstream>
#include <iconv.h>
using namespace std;
#pragma comment(lib,"libIconv.lib")
//编码转换,source_charset是源编码,to_charset是目标编码
std::string code_convert(char *source_charset, char *to_charset, const std::string& sourceStr) //sourceStr是源编码字符串
{
iconv_t cd = iconv_open(to_charset, source_charset);//获取转换句柄,void*类型
if (cd == 0)
return "";
size_t inlen = sourceStr.size();
size_t outlen = 255;
char* inbuf = (char*)sourceStr.c_str();
char outbuf[255];//这里实在不知道需要多少个字节,这是个问题
//char *outbuf = new char[outlen]; 另外outbuf不能在堆上分配内存,否则转换失败,猜测跟iconv函数有关
memset(outbuf, 0, outlen);
char *poutbuf = outbuf; //多加这个转换是为了避免iconv这个函数出现char(*)[255]类型的实参与char**类型的形参不兼容
if (iconv(cd, &inbuf, &inlen, &poutbuf,&outlen) == -1)
return "";
std::string strTemp(outbuf);//此时的strTemp为转换编码之后的字符串
iconv_close(cd);
return strTemp;
}
//gbk转UTF-8
std::string GbkToUtf8(const std::string& strGbk)// 传入的strGbk是GBK编码
{
return code_convert("gb2312", "utf-8",strGbk);
}
//UTF-8转gbk
std::string Utf8ToGbk(const std::string& strUtf8)
{
return code_convert("utf-8", "gb2312", strUtf8);
}
//gbk转unicode,"UCS-2LE"代表unicode小端模式
std::string GbkToUnicode(const std::string& strGbk)// 传入的strGbk是GBK编码
{
return code_convert("gb2312", "UCS-2LE",strGbk);
}
//unicode转gbk
std::string UnicodeToGbk(const std::string& strGbk)// 传入的strGbk是GBK编码
{
return code_convert("UCS-2LE", "gb2312",strGbk);
}
int main()
{
//1、读取"ANSI.txt"
ifstream in("ANSI.txt");
string strGbk;
in>>strGbk;
in.close();
cout<<strGbk<<endl;
int num = strGbk.size();//获取两个字符数,也是我字所占的字节数
unsigned char* p = (unsigned char*)strGbk.c_str();
for (int i = 0; i < num; i++)
{
printf("%0x", *p);
p++;
} //输出ced2 所以我的GBK编码是0xced2
printf("\n");
//2、读取"unicode.txt"
in.open("unicode.txt");
//过滤文本开始ff fe两个字节
char a;
in>>a;
in>>a;
string strUnicode;
in >> strUnicode;
in.close();
cout<<UnicodeToGbk(strUnicode)<<endl;//转成gbk输出
num = strUnicode.size();
p = (unsigned char*)strUnicode.c_str();
for (int i = 0; i < num; i++)
{
printf("%0x", *p);
p++;
} //输出1162 因为默认是小端模式,所以我的unicode编码是0x6211
printf("\n");
//3、读取"utf8.txt"
in.open("utf8.txt");
//过滤文本开始efbbbf三个字节
char b;
in>>b;
in>>b;
in>>b;
string strUtf8;
in>>strUtf8;
in.close();
cout<<Utf8ToGbk(strUtf8)<<endl;//转成gbk输出
num = strUtf8.size();
p = (unsigned char*)strUtf8.c_str();
for (int i = 0; i < num; i++)
{
printf("%0x", *p);
p++;
} //输出e68891
printf("\n");
return 0;
}
ifstream读取utf8文本到string时,需要过滤文本开始efbbbf三个字节,否则转成gbk会失败。
下面是实现代码:
#include <iostream>
#include <string>
#include <fstream>
#include <iconv.h>
using namespace std;
#pragma comment(lib,"libIconv.lib")
//编码转换,source_charset是源编码,to_charset是目标编码
std::string code_convert(char *source_charset, char *to_charset, const std::string& sourceStr) //sourceStr是源编码字符串
{
iconv_t cd = iconv_open(to_charset, source_charset);//获取转换句柄,void*类型
if (cd == 0)
return "";
size_t inlen = sourceStr.size();
size_t outlen = 255;
char* inbuf = (char*)sourceStr.c_str();
char outbuf[255];//这里实在不知道需要多少个字节,这是个问题
//char *outbuf = new char[outlen]; 另外outbuf不能在堆上分配内存,否则转换失败,猜测跟iconv函数有关
memset(outbuf, 0, outlen);
char *poutbuf = outbuf; //多加这个转换是为了避免iconv这个函数出现char(*)[255]类型的实参与char**类型的形参不兼容
if (iconv(cd, &inbuf, &inlen, &poutbuf,&outlen) == -1)
return "";
std::string strTemp(outbuf);//此时的strTemp为转换编码之后的字符串
iconv_close(cd);
return strTemp;
}
//gbk转UTF-8
std::string GbkToUtf8(const std::string& strGbk)// 传入的strGbk是GBK编码
{
return code_convert("gb2312", "utf-8",strGbk);
}
//UTF-8转gbk
std::string Utf8ToGbk(const std::string& strUtf8)
{
return code_convert("utf-8", "gb2312", strUtf8);
}
//gbk转unicode,"UCS-2LE"代表unicode小端模式
std::string GbkToUnicode(const std::string& strGbk)// 传入的strGbk是GBK编码
{
return code_convert("gb2312", "UCS-2LE",strGbk);
}
//unicode转gbk
std::string UnicodeToGbk(const std::string& strGbk)// 传入的strGbk是GBK编码
{
return code_convert("UCS-2LE", "gb2312",strGbk);
}
int main()
{
//1、读取"ANSI.txt"
ifstream in("ANSI.txt");
string strGbk;
in>>strGbk;
in.close();
cout<<strGbk<<endl;
int num = strGbk.size();//获取两个字符数,也是我字所占的字节数
unsigned char* p = (unsigned char*)strGbk.c_str();
for (int i = 0; i < num; i++)
{
printf("%0x", *p);
p++;
} //输出ced2 所以我的GBK编码是0xced2
printf("\n");
//2、读取"unicode.txt"
in.open("unicode.txt");
//过滤文本开始ff fe两个字节
char a;
in>>a;
in>>a;
string strUnicode;
in >> strUnicode;
in.close();
cout<<UnicodeToGbk(strUnicode)<<endl;//转成gbk输出
num = strUnicode.size();
p = (unsigned char*)strUnicode.c_str();
for (int i = 0; i < num; i++)
{
printf("%0x", *p);
p++;
} //输出1162 因为默认是小端模式,所以我的unicode编码是0x6211
printf("\n");
//3、读取"utf8.txt"
in.open("utf8.txt");
//过滤文本开始efbbbf三个字节
char b;
in>>b;
in>>b;
in>>b;
string strUtf8;
in>>strUtf8;
in.close();
cout<<Utf8ToGbk(strUtf8)<<endl;//转成gbk输出
num = strUtf8.size();
p = (unsigned char*)strUtf8.c_str();
for (int i = 0; i < num; i++)
{
printf("%0x", *p);
p++;
} //输出e68891
printf("\n");
return 0;
}
相关文章推荐
- C++ 读取中文文本 ifstream
- ifstream读取文件至string stringstream
- VC与DEV编译器关于ifstream的文本反复读取问题存在的不同
- 能读取Unicode编码格式的ReadString和WriteString函数
- 编码实现读取文本的方法
- delphi读取mysql中编码为GBK的表时乱码的消除
- ifstream或ofstream读取二进制文本乱码问题
- 编码实现读取文本的方法
- ofstream/ifstream 文本/二进制 方式 读入/写出 数据方法
- 编码实现读取文本的方法
- 编码实现读取文本的方法
- C#_读取文本------ 保存文本----功能的实现
- 从stream中读取string 图象也可以直接来源于流
- mfc中如何读取,保存编码为utf-8的文件
- Java 小例子:按指定的编码读取文本文件内容
- ofstream/ifstream 文本/二进制 方式 读入/写出 数据方法
- 编码实现读取文本的方法
- 数据库操作类与Xml文档操作(保存/读取Connectionstring)
- 读取utf-8等编码的文本
- C#读取MySql数据库并保存为xml与文本格式