64,266
社区成员
发帖
与我相关
我的任务
分享
void MyFile::JudgeFile(LPCWSTR lpFileName, LPWSTR lpType)
{
FILE *fp;
if ((fp = _wfopen(lpFileName, L"r")) == NULL)
{
return;
}
BYTE btHead[3];
// 读取文件前3个字节
size_t uRead = fread(btHead, 1, 3, fp);
if (uRead == 3 && btHead[0] == 0xEF && btHead[1] == 0xBB && btHead[2] == 0xBF)
{
swprintf(lpType, L"r, ccs=UTF-8");
return;
}
if (uRead >= 2 && ((btHead[0] == 0xFE && btHead[1] == 0xFF) || (btHead[0] == 0xFF && btHead[1] == 0xFE)))
{
swprintf(lpType, L"r, ccs=UNICODE");
return;
}
swprintf(lpType, L"r");
}
bool MyFile::OpenFile(LPCWSTR lpFileName)
{
FILE *fp;
wchar_t szType[32];
JudgeFile(lpFileName, szType);
if ((fp = _wfopen(lpFileName, szType)) == NULL)
{
return false;
}
wchar_t szLine[MAX_LINE];
while(fgetws(szLine, MAX_LINE, fp))
{
m_file_text += szLine;
m_file_text += L'\n';
std::wcout << szLine << std::endl;
}
if (wcslen(szType) == 2)
{
// 怎样转换?????????
}
else
{
m_lpText = m_file_text.c_str();
}
return true;
}