Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Reading file fa...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 45564 of 48022
Post > Topic >>

Reading file fails with ReadFile

by mkarja <mkarja@[EMAIL PROTECTED] > Apr 27, 2008 at 04:48 AM

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.

----
 




 2 Posts in Topic:
Reading file fails with ReadFile
mkarja <mkarja@[EMAIL   2008-04-27 04:48:04 
Re: Reading file fails with ReadFile
red floyd <no.spam@[EM  2008-04-27 07:09:55 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 11:34:39 CDT 2008.