国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

c++ifstream寫入unicode編碼格式的文本

2019-11-14 09:42:12
字體:
來源:轉載
供稿:網(wǎng)友
#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];//這里實在不知道需要多少個字節(jié),這是個問題	//char *outbuf = new char[outlen]; 另外outbuf不能在堆上分配內(nèi)存,否則轉換失敗,猜測跟iconv函數(shù)有關	memset(outbuf, 0, outlen);	char *poutbuf = outbuf; //多加這個轉換是為了避免iconv這個函數(shù)出現(xiàn)char(*)[255]類型的實參與char**類型的形參不兼容	if (iconv(cd, &inbuf, &inlen, &poutbuf,&outlen) == -1)		return "";    std::string strTemp(outbuf);//此時的strTemp為轉換編碼之后的字符串	iconv_close(cd);	return strTemp;}//gbk轉unicode,"UCS-2LE"代表unicode小端模式std::string GbkToUnicode(const std::string& strGbk)// 傳入的strGbk是GBK編碼 {	return code_convert("gb2312", "UCS-2LE",strGbk);}int main() {       ofstream out("b.txt");    unsigned char head[3] = {0xff,0xfe,0x00};//unicode文件頭,0x00必須加上    out << head ;    string str =GbkToUnicode("我") ;//"我"在vs文本編輯器里默認是gbk編碼,需要通過GbkToUnicode轉成Unicode編碼    out << str;    out.close();   	return 0;}

參照博客:Qt寫入unicode編碼格式的文本


上一篇:又見回文

下一篇:Dijkstra算法

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新竹县| 丹东市| 页游| 嘉荫县| 渝中区| 五大连池市| 扎兰屯市| 富蕴县| 南部县| 河津市| 九台市| 灌阳县| 思茅市| 长寿区| 南澳县| 合水县| 从江县| 鹰潭市| 沁水县| 广东省| 凌海市| 漳州市| 云林县| 开鲁县| 惠来县| 黑龙江省| 元朗区| 新龙县| 广汉市| 华宁县| 江陵县| 河南省| 英吉沙县| 肥西县| 刚察县| 丹阳市| 朝阳市| 赫章县| 涟水县| 鹤壁市| 清丰县|