我用MFC中定义了个char数组,不管是TCHAR,wchar_t,等类型时,输出到记事本时,只要是汉字都出现了乱码,我用的是vs2013,编码采用的是unicode,而记事本用的是ASCII码,因此总能出现这个问题,在网上找到了个这样的办法,首先在头部声明这个宏定义,sizeof(TCHAR)默认值是1,若是是unicode,则是2,
#define TCHARLEN (sizeof(TCHAR)) // 一个字符占的字节数
接下来,
CFile file;
try
{
file.Open(_T("应用程序.txt"), CFile::modeCreate | CFile::modeWrite);
}
catch (CFileException* e)
{
if (e->m_cause == CFileException::fileNotFound)
{
MessageBox(_T("文件不存在!!!"), (_T("警告")), MB_ICONHAND);
return;
}
}
ULONGLONG dwFileLen = file.GetLength();
if (0 == dwFileLen) // 文件为空时写入UNICODE字节序标记
{
const unsigned char LeadBytes[] = { 0xff, 0xfe };
file.Write(LeadBytes, sizeof(LeadBytes));
}
file.Write(&napAdd,sizeof(napAdd));
file.Close();
其中,ULONGLONG dwFileLen = file.GetLength();
if (0 == dwFileLen) // 文件为空时写入UNICODE字节序标记
{
const unsigned char LeadBytes[] = { 0xff, 0xfe };
file.Write(LeadBytes, sizeof(LeadBytes));
}这段代码,使编码不会由于汉字而乱码。
数组