MFC Unicode字符集下 CFile CString UTF-8文件 ANSI 文件

MFC Unicode字符集下 CFile CString UTF-8文件 ANSI 文件

1

读取ANSI编码的文件时,现将文件存储在char* 指向的内存内,而后使用转换将char转换为wchar_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();

2

MultiByteToWideChar

MultiByteToWideChar(CP_ACP, 0, postJsonAlarmCstr2T2A, -1, pszUnicode, dwCount);

WideCharToMultiByte

WideCharToMultiByte(CP_UTF8, 0, pszUnicode, -1, pszUTF8 + sizeof(arUTF8Header), dwCount - sizeof(arUTF8Header), NULL, NULL);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值