当前位置:   article > 正文

MFC读取utf8文件_mfc 读取utf8文件

mfc 读取utf8文件
void CtempDlg::ConvertUTF8ToANSI(CString strUTF8,CString &strANSI)
{
	int nLen = ::MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,(LPCTSTR)strUTF8,-1,NULL,0); 
	//返回需要的unicode长度   
	WCHAR * wszANSI = new WCHAR[nLen+1];   
	memset(wszANSI, 0, nLen * 2 + 2);   
	nLen = MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8, -1, wszANSI, nLen);    //把utf8转成unicode  
	nLen = WideCharToMultiByte(CP_ACP, 0, wszANSI, -1, NULL, 0, NULL, NULL);        //得到要的ansi长度   
	char *szANSI=new char[nLen + 1];   
	memset(szANSI, 0, nLen + 1);   
	WideCharToMultiByte (CP_ACP, 0, wszANSI, -1, szANSI, nLen, NULL,NULL);          //把unicode转成ansi   
	strANSI = szANSI;   
	delete wszANSI;   
	delete szANSI;   
}
 
void CtempDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	
	CStdioFile mFile; 
	CFileException mExcept; 
	mFile.Open(_T("C:\\a.txt"), CFile::modeRead); 
	CString string,str;
	mFile.ReadString(string);
	ConvertUTF8ToANSI(string,str);
	mFile.Close(); 
 
	CDialogEx::OnOK();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/123131
推荐阅读
  

闽ICP备14008679号