ProgrammerJoe
Programmer
I have a Unicode text file. (I can tell because the characters 0xFF and 0xFE are the first two characters in the file and looking at it in a binary editor, I can see that each character is followed by a null character). My problem is this: when I use ReadString, it takes each individual byte from the file and expands it into a two-byte character. So each of the lines that I read in are only one character long, since the second character is a null character. How can I read the Unicode file? I've been tempted to read the file byte-by-byte and copy into memory typecast as a pointer to an array of wchar_t, since it's the only solution I can come up with. Surely there's got to be something more elegant than that.
Here's my code for reading the file (in two ways!):
Here's my code for reading the file (in two ways!):
Code:
void CodeSnippet()
{
CStdioFile MFCFile;
CString CurrLine;
FILE* RegularFile;
wchar_t UnicodeLine[200];
if (!MFCFile.Open(_T("c:\\temp\\unicode.txt"), CFile::modeRead | CFile::typeText))
{
MessageBox(NULL, _T("Cannot open file"), _T("Failure"), MB_ICONEXCLAMATION);
}
while (MFCFile.ReadString(CurrLine))
{
MessageBox(NULL, CurrLine, _T("Line Read"), MB_ICONINFORMATION);
}
MFCFile.Close();
// alternate way
RegularFile = fopen("c:\\temp\\unicode.txt", "r");
if (!RegularFile) {
fgetws(UnicodeLine, 100, RegularFile);
MessageBox(NULL, UnicodeLine, _T( "Line Read"), MB_ICONINFORMATION);
}
fclose(RegularFile);
}