有一UTF-8的xml文件,要读取文件内容并存入一CString对象或字符串数组中,以下是本人写的代码
CString szReturn;
CFile file;
file.Open(“C:\Trans.xml”, CFile::modeRead);
int FileSize = file.GetLength();
char *pBuf;
pBuf = new char[4096];
memset(pBuf, 0, 4096);
file.Read(pBuf, FileSize);
file.Close();
wchar_t *pwcReturn;
int iLen = MultiByteToWideChar(CP_UTF8, 0, pBuf, FileSize+1, NULL, 0);
pwcReturn =new wchar_t[iLen];
MultiByteToWideChar(CP_UTF8, 0, pBuf, FileSize+1, pwcReturn, iLen);
WideCharToMultiByte(CP_ACP, 0, pwcReturn, iLen+1, szReturn.GetBuffer(0), 4096, NULL, NULL);
问一下有没有简便一点的方法呢?而且存入字符串数组本人还没搞定,中文一直是乱码…
CString szReturn;
CFile file;
file.Open(“C:\Trans.xml”, CFile::modeRead);
int FileSize = file.GetLength();
char *pBuf;
pBuf = new char[4096];
memset(pBuf, 0, 4096);
file.Read(pBuf, FileSize);
file.Close();
wchar_t *pwcReturn;
int iLen = MultiByteToWideChar(CP_UTF8, 0, pBuf, FileSize+1, NULL, 0);
pwcReturn =new wchar_t[iLen];
MultiByteToWideChar(CP_UTF8, 0, pBuf, FileSize+1, pwcReturn, iLen);
WideCharToMultiByte(CP_ACP, 0, pwcReturn, iLen+1, szReturn.GetBuffer(0), 4096, NULL, NULL);
问一下有没有简便一点的方法呢?而且存入字符串数组本人还没搞定,中文一直是乱码…
解决方案:40分
#include <stdio.h> #include <afxdisp.h> int main() { CFile file; if (!file.Open("C:\Trans.xml", CFile::modeRead)) return 1;; int FileSize = file.GetLength(); char *pBuf; pBuf = new char[FileSize]; file.Read(pBuf, FileSize); file.Close(); int iLen = MultiByteToWideChar(CP_UTF8, 0, pBuf, FileSize, NULL , 0 ); wchar_t *pwcReturn; pwcReturn =new wchar_t[iLen]; MultiByteToWideChar(CP_UTF8, 0, pBuf, FileSize, pwcReturn, iLen); int iBLen= WideCharToMultiByte(CP_ACP, 0, pwcReturn, iLen, NULL , 0 , NULL, NULL); CString szReturn; WideCharToMultiByte(CP_ACP, 0, pwcReturn, iLen, szReturn.GetBuffer(iBLen), iBLen, NULL, NULL); szReturn.ReleaseBuffer(iBLen); printf("[%s]",szReturn); return 0; }