vc 读写txt文件 支持Ascii/Unicode/Unicode big endian/UTF-8

来源:互联网 发布:mysql如何更改密码 编辑:程序博客网 时间:2024/06/06 10:47

 

#include <string>using std::string;using std::wstring;std::wstring GlobalMultiByteToWideChar(const char* pszMultiByte,UINT uCodePage = CP_ACP ){try{if (NULL == pszMultiByte){throw -1;}int iMultiBytes = ::MultiByteToWideChar( uCodePage, 0, pszMultiByte, -1, NULL, 0 );if (iMultiBytes == 0){throw -1;}wchar_t* pwszMultiByte = new wchar_t[ iMultiBytes + 1];pwszMultiByte[iMultiBytes] = 0;if (!pwszMultiByte){throw -1;}::MultiByteToWideChar( uCodePage, 0, pszMultiByte , -1, pwszMultiByte, iMultiBytes );std::wstring wstrWideChar = pwszMultiByte;delete [] pwszMultiByte;pwszMultiByte = NULL;return wstrWideChar.c_str();}catch (...){return L"";}}std::string GlobalWideCharToMultiByte(const wchar_t* pwszMultiByte,UINT uCodePage  = CP_ACP ){try{if (NULL == pwszMultiByte){throw -1;}int iMultiBytes = ::WideCharToMultiByte( uCodePage, 0, pwszMultiByte, -1, NULL, 0 ,NULL,FALSE);if (iMultiBytes == 0){throw -1;}char* pszMultiByte = new char[ iMultiBytes + 1];pszMultiByte[iMultiBytes] = 0;if (!pszMultiByte){throw -1;}::WideCharToMultiByte( uCodePage, 0, pwszMultiByte , -1, pszMultiByte, iMultiBytes ,NULL,FALSE);std::string strMultiChar = pszMultiByte;delete [] pszMultiByte;pszMultiByte = NULL;return strMultiChar.c_str();}catch (...){return "";}}//////////////////////////////////////////////////////////////////////////enum BomFormat{BOM_ASCII,BOM_UNICODE,BOM_UNICODE_BIG_ENDIAN,BOM_UTF_8};bool SaveTxtData(std::wstring& strText,std::wstring& strPathName,BomFormat bomFormat = BOM_UNICODE){CFile File;bool res = false ;if (File.Open(strPathName.c_str(), CFile::modeCreate | CFile::modeWrite | CFile::typeBinary)){unsigned char *lpData = NULL;unsigned long ulLen = 0 ;unsigned long ulDataLen = strText.length() * 2 ;switch (bomFormat){case BOM_ASCII:{std::string strData = GlobalWideCharToMultiByte(strText.c_str());ulLen = strData.length();lpData = new unsigned char[ulLen];memcpy(lpData,strData.c_str(),ulLen);}break;case BOM_UNICODE:{ulLen = ulDataLen + 2 ;lpData = new unsigned char[ulLen];lpData[0] = 0xff;lpData[1] = 0xfe;memcpy(&lpData[2],strText.c_str(),ulDataLen);}break;case BOM_UNICODE_BIG_ENDIAN:{ulLen = ulDataLen + 2 ;lpData = new unsigned char[ulLen];lpData[0] = 0xfe;lpData[1] = 0xff;wchar_t* pd = new wchar_t[ulDataLen];memcpy(pd,strText.c_str(),ulDataLen);for (unsigned long i = 0;i < ulDataLen ;i ++){pd[i] = htons(pd[i]);}memcpy(&lpData[2],pd,ulDataLen);delete [] pd;}break;case BOM_UTF_8:{std::string strData = GlobalWideCharToMultiByte(strText.c_str(),CP_UTF8);ulLen = strData.length() + 3;lpData = new unsigned char[ulLen];lpData[0] = 0xef;lpData[1] = 0xbb;lpData[2] = 0xbf;memcpy(&lpData[3],strData.c_str(),strData.length());}break;}if (lpData){File.Write(lpData,ulLen);res = true ;}File.Close();}return res;}BomFormat GetTxtBomFormat(unsigned char* lpData,unsigned long ulLen){BomFormat bomFormat = BOM_ASCII ;if ( ulLen >= 2 ){if (lpData[0] == 0xff && lpData[1] == 0xfe) // unicode{bomFormat = BOM_UNICODE;}if (lpData[0] == 0xfe && lpData[1] == 0xff) // unicode big endian{bomFormat = BOM_UNICODE_BIG_ENDIAN; }if (ulLen >= 3){if (lpData[0] == 0xef && lpData[1] == 0xbb && lpData[2] == 0xbf) // utf-8{bomFormat = BOM_UTF_8;}}}return bomFormat;}bool OpenTxtData(std::wstring& strText,std::wstring& strPathName){CFile File;bool res = false ;if (File.Open(strPathName.c_str(), CFile::modeRead | CFile::typeBinary)){int iLength = (int)File.GetLength();unsigned char* szBuffer= new unsigned char[iLength+1];memset(szBuffer, 0, iLength+1);File.Read(szBuffer,iLength);szBuffer[iLength] = 0 ;if (iLength > 0){switch (GetTxtBomFormat(szBuffer,iLength)){case BOM_ASCII:strText = GlobalMultiByteToWideChar((const char*)&szBuffer[0],CP_ACP);break;case BOM_UNICODE:{ASSERT(!(iLength % 2));int len = iLength/2;wchar_t* buf= new wchar_t[len];memset(buf,0,iLength);memcpy(buf,&szBuffer[2],iLength-2);buf[len-1] = 0;strText = buf;delete [] buf;}break;case BOM_UNICODE_BIG_ENDIAN:{ASSERT(!(iLength % 2));int len = iLength/2;wchar_t* buf= new wchar_t[len];memset(buf,0,iLength);memcpy(buf,&szBuffer[2],iLength-2);for (int i = 0;i < len;i ++){buf[i] = htons(buf[i]);}buf[len-1] = 0;strText = buf;delete [] buf;}break;case BOM_UTF_8:strText = GlobalMultiByteToWideChar((const char*)&szBuffer[3],CP_UTF8);break;}}res = true ;delete [] szBuffer;File.Close();}return res;}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 长得丑不敢相亲怎么办 abs抱死发不动了怎么办 qq飞车手游车到期了改装怎么办 育碧下载游戏慢怎么办 豹纹守宫鼻子还有个残皮怎么办 睫角守宫断尾怎么办 定金发票弄丢了怎么办 xp电脑显示输入不支持怎么办 月子见风怎么办暴雪将 老氏电视机出现雪花点怎么办 飞利浦电视显示雪花片怎么办 电视无信号出雪花怎么办 电视打开都是雪花没有电视台怎么办 电视视频1无信号怎么办 新买的电视无信号怎么办 卫星有信号没有视频怎么办 户户通没有信号怎么办视频 雪花泥粘衣服上怎么办 遗产按份额处分判决后怎么办 宝马后驱车下雪天怎么办 宝马后驱车路滑怎么办 车子陷入泥地里怎么办 深圳居住证签注过期了怎么办 手机不能播放视频乱码了怎么办 被加密的视频无法观看怎么办 苹果5忘记id密码怎么办 七个月宝宝脾胃不好怎么办 八个月宝宝脾虚怎么办 七个月宝宝脾胃虚怎么办 海岛奇兵点错了怎么办 螳螂的脚断了怎么办 海岛奇兵打不过玩家怎么办 海岛奇兵资源满了怎么办 海岛奇兵杯越来越多打不玩家怎么办 海岛奇兵控杯技巧 杯数太高怎么办 海岛奇兵发现求救信号怎么办 海岛奇兵被打了怎么办 小鱼翅卡喉咙了怎么办 鱼翅卡在喉咙里怎么办 斗鱼身份证被使用怎么办 做的鱼丸太腥了怎么办