Click to See Complete Forum and Search --> : ReadFile / Reading a text file


ScanMan
January 15th, 2008, 02:32 PM
I'm looking for help and a little understanding, mostly as I dive headfirst into C++ and try and wrap my head around types and conversions involving windows programming.

Specifically, I have a .txt file from Notepad, currently "123456", eventually it'll be a variable length comma delimited file. I'd like to pull the whole file in and parse it apart later. Part of what will happen with the values is display in a ListBox in a PocketPC app.

If I read one character at a time, as in szBuf1 and szBuf2 below, I can then display them in the ListBox, but multiple characters at once (what I'm trying with szBuf3) gets me a couple random squares displayed on the screen.



void ReadDataFile(HWND hDebugList)
{
DWORD nBytesToRead = 0;
DWORD nBytesRead = 0;
BOOL bResult = FALSE;


// Create file
g_hFile = CreateFile(g_filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (INVALID_HANDLE_VALUE == g_hFile)
{
ReportLastError();
return;
}
else
{
ListBox_AddString(hDebugList, TEXT("CreateFile succeed"));
}

// Read file
nBytesToRead = GetFileSize(g_hFile, NULL);
if (nBytesToRead <=0)
{
ListBox_AddString(hDebugList, TEXT("Empty File!"));
return;
}

TCHAR szBuf1[2] = TEXT("");
TCHAR szBuf2[2] = TEXT("");
TCHAR szBuf3[4] = TEXT("");
TCHAR szTemp[20]= TEXT("");

//quick debug output
ListBox_AddString(hDebugList, TEXT("file size"));
_ltot(nBytesToRead, szTemp, 10);
ListBox_AddString(hDebugList, szTemp);

bResult = ReadFile(g_hFile, szBuf1, 1, &nBytesRead, NULL);
bResult = ReadFile(g_hFile, szBuf2, 1, &nBytesRead, NULL);
bResult = ReadFile(g_hFile, szBuf3, 4, &nBytesRead, NULL);
if (bResult && (nBytesRead == 0))
{
// End of file
}
else if (!bResult)
{
ListBox_AddString(hDebugList, TEXT("File not read"));
}
else
{
ListBox_AddString(hDebugList, TEXT("File read"));
ListBox_AddString(hDebugList, szBuf1);
ListBox_AddString(hDebugList, szBuf2);
ListBox_AddString(hDebugList, szBuf3);

}
CloseHandle(g_hFile);

}


I'm guessing this is just me not understanding the different types and conversions well enough. Any help would be appreciated to get szBuf3 to display properly, as well as what type of variable I should be bringing my file in to that will be easy to work with later.

Thanks in advance for any help.

kirants
January 15th, 2008, 02:44 PM
My guess is, your file is using ANSI encoding. However, Pocket PC uses UNICODE data for strings. Result is that , when you use szBuf3, and pull in 4 bytes into it, Listbox sees it as 2 characters and interprets is wrongly.

ScanMan
January 16th, 2008, 10:33 AM
Thanks for the response. I eventually ran across an article by Nancy Nicolaisen entitled "Reading And Writing Files From WinCE Apps
" right on this site that explains exactly what the problem was, and shows, in short:


how to read a multibyte character set (MBCS) text and convert it to Unicode for display in an edit control.