// 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
csText = (LPCWSTR)(&pBuffer[2]);
csNotes += _T("File starts with hex FFFE, assumed to be wide char format. ");
}
else
{
// _UNICODE builds should perform UTF-8 to UCS-2 (wide char) conversion here
csText = (LPCSTR)pBuffer;
}
CString csText;
CString csNotes;
CFile file;
if ( ! file.Open( csFilename, CFile::modeRead ) )
{
AfxMessageBox(_T("unable to open file"));
return;
}
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
csText = (LPCWSTR)(&pBuffer[2]);
csNotes += _T("File starts with hex FFFE, assumed to be wide char format. ");
}
else
{
// _UNICODE builds should perform UTF-8 to UCS-2 (wide char) conversion here
csText = (LPCSTR)pBuffer;
}
delete [] pBuffer;
// If it is too short, assume it got truncated due to non-text content
if ( csText.GetLength() < nFileLen / 2 - 20 )
{
AfxMessageBox( _T("Error converting file to string (may contain binary data)") );
return;
}