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();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值