I want to fill an edit control from a text file. I need help debugging
a couple of runtime errors: "Stack around the variable txtFromFile was
corrupted" and an access violation in ReadFile(). Ideas?
/////////////////////////////////////////////////////////////////////////////////
char szFileName[MAX_PATH] = "";
LPCSTR txtFromFile;
DWORD bytestoread = 1024;
DWORD bytesread;
if (LOWORD(wParam) == ID_BROWSE)
{
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hDlg;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files
(*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST |
OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt";
if (GetOpenFileName(&ofn))
{
HANDLE hFile = CreateFile(szFileName, GENERIC_READ, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (ReadFile(hFile, &txtFromFile, bytestoread, &bytesread,
NULL))
{
SetDlgItemText(hDlg, IDC_EDIT1, txtFromFile);
}
CloseHandle(hFile);
return true;
}
}
/////////////////////////////////////////////////////////////////////////////////
Jochen Kalmbach [MVP] - 02 Jul 2005 19:09 GMT
Hi Jon!
> I want to fill an edit control from a text file. I need help debugging
> a couple of runtime errors: "Stack around the variable txtFromFile was
[quoted text clipped - 4 lines]
> if (ReadFile(hFile, &txtFromFile, bytestoread, &bytesread,
> NULL))
Where do you store the read-data?
txtFromFile is just a pointer...
You need to do something like:
char *szBuffer[1024];
DWORD bytesToRead = 1024;
Now you can pass "szBuffer" to ReadFile:
ReadFile(hFile, szBuffer, bytesToRead, &bytesRead, NULL)

Signature
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Jon - 03 Jul 2005 00:00 GMT