Skip to main content
GameDev.net gamedev.net
🔒 Locked

CreateFile(); ReadFile(); WriteFile();

Started by CtrlAltDel Dec 9, 2002 at 7:10 PM 7 replies 250+ views
Original Post
CtrlAltDel
CtrlAltDel
I am trying to write a few functions that write and load a map. However, the function does not error under MSVC++, but after I run the write function, it creates the file but the size is 0 bytes. int x,y; HANDLE map; char fp[10] = "World.map"; map=CreateFile(fp,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); if(WriteFile(map,(void *)x,sizeof(int),NULL,NULL)) { } if(WriteFile(map,(void *)y,sizeof(int),NULL,NULL)) { } CloseHandle(map); -And to load the map: void LoadMap() { HANDLE map; char fp[10] = "World.map"; map=CreateFile(fp,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); ReadFile(map,(void *)mapSizeX, sizeof(int),NULL,NULL); ReadFile(map,(void *)mapSizeZ, sizeof(int),NULL,NULL); CloseHandle(map); } So in short it doesn't write to a file, just creates one...so there is nothing to read(but I am sure I screwed up on that part too.) Any Ideas? [edited by - CtrlAltDel on December 9, 2002 8:12:14 PM] [edited by - CtrlAltDel on December 9, 2002 8:18:22 PM]
Evil Bill
Evil Bill
Make sure you close the file handle after you''ve written to it (via CloseHandle()). Otherwise the changes won''t be saved, so you''ll end up with a file 0 bytes in size...

Member of the Unban Mindwipe Society (UMWS)
CtrlAltDel
CtrlAltDel
Yeah, Seems I forgot to copy/paste that part. I have it in the code and yet it still doesn''t write anything under MCVC++ 6 running XP.
Shannon Barber
Shannon Barber
FILE_SHARE_WRITE means other files can open it and write to it, while you are - use FILE_SHARE_READ or 0. I do not think this is the source of your problem though...

Check the handle value against INVALID_HANDLE_VALUE to see it is actually suceeds in creating the file.

You may have to supply a variable address for the second-to-last parameter of WriteFile. It the version of the docs I checked, it doesn''t say that NULL is an acceptable option - same with ReadFile.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
niyaw
niyaw
you forgot to take addresses of variables x, y, mapSizeX, mapSizeY.
CtrlAltDel
CtrlAltDel

int x,y;
int* px=&x
int* py=&y

WriteFile(map,(void *)px,sizeof(int),NULL,NULL))


Is that what you mean?
niyaw
niyaw
you can just do (incorporating Magmai''s correction):

DWORD written;
WriteFile(map,&x,sizeof(int),&written,NULL))

win9x wants fourth parameter to point somewhere.
CtrlAltDel
CtrlAltDel
Thanks it works...

[edited by - CtrlAltDel on December 9, 2002 8:58:42 PM]
Jan Wassenberg
Jan Wassenberg
Read the dox:
Windows NT/2000/XP: If lpOverlapped is NULL, lpNumberOfBytesWritten cannot be NULL.
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.