16,548
社区成员




bool C****::ReadFile(const CString& xmlFile, CString& xmlText)
{
// This code derived from Ben Bryant's MarkupDlg.cpp file.
// Open file.
CFile file;
if ( !file.Open(xmlFile, CFile::modeRead) )
{
return false;
}
int nFileLen = (int)file.GetLength();
// Allocate buffer for binary file data.
unsigned char* pBuffer = new unsigned char[nFileLen + 2];
nFileLen = file.Read( pBuffer, nFileLen );
file.Close();
pBuffer[nFileLen] = '\0';
pBuffer[nFileLen+1] = '\0'; // in case 2-byte encoded
// Windows Unicode file is detected if starts with FEFF.
if ( pBuffer[0] == 0xff && pBuffer[1] == 0xfe )
{
// Contains byte order mark, so assume wide char content.
// Non _UNICODE builds should perform UCS-2 (wide char) to UTF-8 conversion here.
xmlText = (LPCWSTR)(&pBuffer[2]);
}
else
{
// _UNICODE builds should perform UTF-8 to UCS-2 (wide char) conversion here.
xmlText = (LPCSTR)pBuffer;
}
delete [] pBuffer;
// If it is too short, assume it got truncated due to non-text content.
if ( xmlText.GetLength() < nFileLen / 2 - 20 )
{
return false;
}
return true;
}