全部都是论坛上找到的代码,自己整理了一下,主要目的是方便以后自己查找使用。
- 判断文件是Unicode编码还是ANSI编码
int IsUnicodeFile(PTSTR pstrName)//这个函数是判断函数,()内是文件路径,如果是Unicode则该函数值是1
{
try
{
CFile file(pstrName, CFile::modeRead);
BYTE ch[100];
memset(ch, 0, 100);
int ii = file.Read(ch, 100);
file.Close();
int i = 0;
int iU = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE;
if (IsTextUnicode(ch, 100, &iU))//是否是unicode文件
{
return 1;
}
return 0;
}
catch (CException* e)
{
return -1;
}
return 0;
}
2.如果是Unicode文件则转化成ANSI编码文件
void CViewdataDlg::UnicodeFile2ANSIFile()//函数功能:如果是Unicode文件则把文件转换成ansi格式文件
{
if (IsUnicodeFile(_T("")) == 1)//双引号里为文件路径
{
/*********************打开文件*********************************************/
CFile file;
CFileException e;
TCHAR* pszFileName = _T("");//双引号里为文件路径
if (!file.Open(pszFileName, CFile::modeReadWrite, &e))
{
TRACE(_T("File could not be opened %d\n"), e.m_cause);
}
//MessageBox(_T("打开成功"));
/*********************读取文件*********************************************/
LONGLONG wfilelen = file.GetLength();
TCHAR *wp = new TCHAR[wfilelen + 1];
memset(wp, 0,