CString CSocketClientDlg::GotFile(CString filename)
{
CFile fileR;
CString strFile="";
if(!fileR.Open(filename,CFile::modeRead|CFile::typeBinary))
{
return strFile;
}
BYTE head[3];
fileR.Read(head,3);
if(!(head[0]==0xEF && head[1]==0xBB && head[2]==0xBF))
{
fileR.SeekToBegin();
}
ULONGLONG FileSize=fileR.GetLength();
char* pContent=(char*)calloc(FileSize+1,sizeof(char));
fileR.Read(pContent,FileSize);
fileR.Close();
int n=MultiByteToWideChar(CP_UTF8,0,pContent,FileSize+1,NULL,0);
wchar_t* pWideChar=(wchar_t*)calloc(n+1,sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,pContent,FileSize+1,pWideChar,n);
strFile=CString(pWideChar);
free(pContent);
free(pWideChar);
return strFile;
}
CFile 读取 UTF-8
最新推荐文章于 2022-06-12 11:08:10 发布
该段代码展示了如何使用CFile类从UTF-8编码的文件中读取内容。首先检查文件头是否为UTF-8 BOM,然后读取整个文件到缓冲区,再将缓冲区的内容转换为宽字符字符串,最后返回转换后的字符串。
摘要由CSDN通过智能技术生成