473,320 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Reading file fails with ReadFile

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.

----
Jun 27 '08 #1
1 2094
mkarja wrote:
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-lit...t.html#faq-5.9
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Benjamin de Waal | last post by:
Hey all, I'm trying to figure out how to directly write to a device in Windows. Basically, what I'm wanting to do is create an image of a device (specifically, a CompactFlash card that uses a...
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
4
by: Jason Kumpf | last post by:
OK I've been staring at this code all day and still with everything I have tried I cannot figure out two problems I am having. Once is why the space limit for the directory I create in the code...
8
by: Andrew Robert | last post by:
Hi Everyone. I tried the following to get input into optionparser from either a file or command line. The code below detects the passed file argument and prints the file contents but the...
2
tshabza
by: tshabza | last post by:
Could anyone give me idea on how to read a wave and convert in into an integer array file using C++, for now I can successfullly read text file.
5
by: Anja | last post by:
Hi everyone, I have a question about text file reading with VBA. I want to read he whole contents of the file in one string variable. I have been able to successfully read lines using: Line...
3
by: scorro1 | last post by:
Hey guys, I am working on a program which is supposed to read input from a file at 100 chars per time, then change the spaces to underscores and reverse the order of the entire document. Now we...
2
by: doublemaster007 | last post by:
Hi How to read binary files in MAC OS? FILE *readFile = fopen("filename", "rb"); then reading it like this: count=fread(readBuffer, sizeof(char), bufferSize, readFile)
4
by: lilyumestar | last post by:
I have project I have to do for class. We have to write 4 different .java files. Project2.java HouseGUI.java House.java HouseSorting.java I already finish House.java and I need to work on...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.