首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

CArchive写字符串出现乱码解决思路

2013-01-25 
CArchive写字符串出现乱码用CArchive往.TXT里写字符串时,第一个字符是乱码。据说是因为使用UNICODE编码时,

CArchive写字符串出现乱码
用CArchive往.TXT里写字符串时,第一个字符是乱码。据说是因为使用UNICODE编码时,自动加入两个二进制前缀0xff和0xfe,但是我的代码改过之后还是不行:

CString str1;
str1= _T("../测试成绩/")+m_strName+_T(".txt");  //给txt起名字
CFile file1(str1, CFile::modeReadWrite|CFile::modeCreate);
 CArchive ar(&file1, CArchive::store);
ar<<(byte)0xff<<(byte)0xfe;//这个是网上的方法,加过之后没有效果
    ar<<m_strInfo;//这个是正常要写入的数据,strInfo是一组字符和数字的组合
ar.Close();
 file1.Close();
 exit (0);

这样在.txt里的第一个字符会是乱码,后面正常。
大神求助。
[解决办法]

CString str1;
str1=_T("../测试成绩/")+m_strName+_T(".txt");//给txt起名字
CFile file1(str1,?CFile::modeReadWrite
[解决办法]
CFile::modeCreate);
char prefix[2] = {0xff, 0xfe};//Unicode文件头
file1.Write(prefix, 2);
file1.Write(str.GetBuffer(),str.GetLength()*sizeof(TCHAR));  
file1.Close();

热点排行