可以自动读取文本文件的编码
typedef enum TextFileType
{
TextFileType_ANSI = 0,
TextFileType_UNICODE,
TextFileType_UTF8
}TEXTFILETYPE;
TEXTFILETYPE xPub_GetTextFileType(const CString& strFileName, CString& tFileName)
{
TEXTFILETYPE fileType = TextFileType_ANSI;
CFile cfile;
if (cfile.Open(strFileName, CFile::modeRead | CFile::shareDenyWrite))
{
tFileName = cfile.GetFileName();
char buf[3];
cfile.Read(buf, 3);
if ((unsigned char)buf[0] == 0xFF
&& (unsigned char)buf[1] == 0xFE)
{
fileType = TextFileType_UNICODE;
}
else if ((unsigned char)buf[0] == 0xEF
&& (unsigned char)buf[1] == 0xBB
&& (unsigned char)buf[2] == 0xBF)
{
fileType = TextFileType_UTF8;
}
cfile.Close();
}
return fileType;
}