Read text file ??
-
I have a text file abc.txt in Windows 2000, like that: "a b c", export it to emulator. Now I want to read it by these code:
CFile f; CFileException e; PTCHAR szFileName= _T("abc.txt"); if (f.Open(szFileName,CFile::modeRead,&e)) { CString strText; TCHAR szChar[255]; UINT nRead=f.Read(szChar,2); if (SUCCEEDED(nRead)) { CString strMsg; strMsg.Format(_T("Read text: %s"),szChar); MessageBox(strMsg); } }
But messagebox shows a strange string. It's not "a " as I thounght. Why ? ======================= Nothing is perfect -
I have a text file abc.txt in Windows 2000, like that: "a b c", export it to emulator. Now I want to read it by these code:
CFile f; CFileException e; PTCHAR szFileName= _T("abc.txt"); if (f.Open(szFileName,CFile::modeRead,&e)) { CString strText; TCHAR szChar[255]; UINT nRead=f.Read(szChar,2); if (SUCCEEDED(nRead)) { CString strMsg; strMsg.Format(_T("Read text: %s"),szChar); MessageBox(strMsg); } }
But messagebox shows a strange string. It's not "a " as I thounght. Why ? ======================= Nothing is perfectFirst, I think that there is no
0 terminator
at the end of the string (szRead[3] = _T('\0');
)! Second, I think that in the ASCII file (abc.txt) there are no UNICODE characters, but in your Pocket PC application you read UNICODE characters (TCHAR
's). Daniel ;) --------------------------- Never change a running system! -
First, I think that there is no
0 terminator
at the end of the string (szRead[3] = _T('\0');
)! Second, I think that in the ASCII file (abc.txt) there are no UNICODE characters, but in your Pocket PC application you read UNICODE characters (TCHAR
's). Daniel ;) --------------------------- Never change a running system! -
If you have the following 3 characters
abc
in the file"abc.txt"
, try this:#include <atlbase.h> // need for A2CT macro
CFile f;
CFileException e;if (f.Open(_T("abc.txt"),CFile::modeRead,&e))
{
USES_CONVERSION; // need for A2CT macrochar szChar\[255\] = { 0 }; UINT nRead = f.Read(szChar,3); // Read the 3 characters 'abc' szChar\[nRead\] = '\\0'; // Add the NULL terminator CString strMsg; strMsg.Format(\_T("Read text: %s"), A2CT(szChar)); // Ansi --> UNICODE AfxMessageBox(strMsg);
}
Daniel ;) --------------------------- Never change a running system!