Reading file fails with ReadFile

M

mkarja

Hi,

I have a windows MDI program that draws some shapes that can
be saved into a file and read from that file.
The save seems to work with the WriteFile function, but for some
reason
the ReadFile function fails. These WriteFile/ReadFile are win32 api
functions.

I'll post both, the read and write function codes here so you can take
a
look at and perhaps see what the problem is.

---- code WriteFile start -----
BOOL WriteDrzFile(HWND hwnd, LPSTR pstrFile, PTHINGZDATA pThingzData)
{
HANDLE theFile;
BOOL bSuccess = FALSE;

DWORD dwSize;

char szTmp[255], szOut[255];
PtrThingzShapeSave thingzSaveData;

pThingzData->pController->shapez = pThingzData->pController-
_model.GetShapes ();
thingzSaveData = pThingzData->pController->shapez.thingzSaveData;

theFile = CreateFile(pstrFile, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

if (theFile != INVALID_HANDLE_VALUE)
{
DWORD structLength;
structLength = sizeof(thingzSaveData);
if (structLength > 0)
{
RECT rect;
DWORD dwBufferSize = structLength + 1;

if (pThingzData != NULL)
{
DWORD dwWritten;

if(WriteFile(theFile, thingzSaveData, structLength, &dwWritten,
NULL))
{
bSuccess = TRUE;
}
}
}
CloseHandle(theFile);
}
return bSuccess;
}
---- code WriteFile end -----

---- code ReadFile start -----
BOOL ReadDrzFile(HWND hwnd, LPSTR pstrFile, static Controller * pCtrl)
{
HANDLE theFile;
DWORD dwSize;
char szTmp[255], szOut[255];
PtrThingzShapeSave thingzSaveData;
PTHINGZDATA pThingzData;

pThingzData->pController = pCtrl;

theFile = CreateFile(pstrFile, GENERIC_READ, FILE_SHARE_DELETE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, NULL);

if (theFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;

dwFileSize = GetFileSize(theFile, NULL);
if(dwFileSize != 0xFFFFFFFF)
{
if (pThingzData != NULL)
{
DWORD dwRead;

if(ReadFile(theFile, thingzSaveData, dwFileSize,
&dwRead, NULL))
{
pThingzData->pController->ShapeDrawFile(thingzSaveData, hwnd);
}
}
}
CloseHandle(theFile);
}
return FALSE;
}
---- code ReadFile end -----

In the code the thingzSaveData is a structure which holds the saved
elements.
If there's some more explanations needed, I will try to answer any
questions to
make it clearer.

Thanks in advance.

----
 
R

red floyd

mkarja said:
Hi,

I have a windows MDI program that draws some shapes that can
be saved into a file and read from that file.
The save seems to work with the WriteFile function, but for some
reason
the ReadFile function fails. These WriteFile/ReadFile are win32 api
functions.

I'll post both, the read and write function codes here so you can take
a
look at and perhaps see what the problem is.

And this question, which relates only to the Win32 API is off-topic here.

Try a newsgroup with Windows or Microsoft in its title.

Please see FAQ 5.9,
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top