MFC在Unicode字符集下读写ANSI编码文件

读取ANSI编码文件时,先将文件存储在char*指向的内存内,而后使用转换将char*转换为w_char_t*。wchar_t*可以使用CString的

Format函数。

CFile file(_T("test.txt"), CFile::modeRead);//读ANSI编码的文件
int filelen = file.GetLength();
char *p = new char[filelen + 1];
file.Read(p, filelen);
p[filelen] = '\0';
USES_CONVERSION;//转化为wchar_t* 可以使用CString的Format函数。
wchar_t* wp = new wchar_t[filelen + 1];
wp = A2T(p);
str.Format(_T("%s"), wp)
file.close();

写ANSI文件时,将Unicode编码的CString转化为ANSI编码的CStringA,要获取ANSI格式的字符只需要使用

GetBuffer即可。也可使用WideCharToMultiByte转换。

CStringA str_ANSI(str.GetBuffer(0));
CFile file_ANSI(_T("test_ANSI.txt"), CFile::modeCreate | CFile::modeWrite);//写ANSI编码的文件
file_ANSI.Write(str_ANSI.GetBuffer(), str_ANSI.GetLength());
 
//CFile file_ANSI(_T("test_ANSI.txt"), CFile::modeCreate | CFile::modeWrite);
//file_ANSI.Write(p, filelen);
file_ANSI.close();

猜你喜欢

转载自blog.csdn.net/xueluowutong/article/details/81903665
今日推荐