PLEASE help with my "Array" - problem.

Started by
3 comments, last by MindWipe 23 years, 9 months ago
I''m making a Mario-type game. I have a map: Map[200][15] I made an editor. That saves/loads the map like this: void SAVE_MAP(char *fname) { FILE *fptr; fptr = fopen(fname,"wt"); fwrite(Map,(MAP_ARRAY_X*MAP_ARRAY_Y),1,fptr); fclose(fptr); } void LOAD_MAP(char *fname) { FILE *fptr; fptr = fopen(fname,"rt"); fread(Map,(MAP_ARRAY_X*MAP_ARRAY_Y),1,fptr); fclose(fptr); } I define MAP_ARRAY_X and MAP_ARRAY_Y like this: #define MAP_ARRAY_X 200 #define MAP_ARRAY_Y 15 BUT, my problem is. That my editor still only saves 50x15 tiles Not 200x15 Tiles. When I tried to change: #define MAP_ARRAY_X 200, to 50. It only saved 12x15. And when I defined it to 800 it saved much more then before. My guess is 200(Havn''t counted) I just can''t figure out the problem. Map[200][15] is an "int" if it help to know. I''m a very bad programmer, but good at math. And have many ideas. Please help. MindWipe
"To some its a six-pack, to me it's a support group."
Advertisement
I think the problem is when I save. That the error comes when the arrays two intergers are different. Havn''t tried it tough.

What do you think?
"To some its a six-pack, to me it's a support group."
Nope - didn''t work when I made the map 200 x 200. It still only loads 50x15 tiles
"To some its a six-pack, to me it's a support group."
I think that the error is here:

fptr = fopen(fname,"wt");

You must open the file for binary access, and you''re opening it in text mode. Every 0Dh character that you write to the file is changed to 0Dh 0Ah bytes, and others similar changes.

For opening a file in binary mode you must include the "b" mode:

fptr = fopen(fname,"wtb");

I had that error and it took me time to solve it, it''s a typical silly error 8)

Bye
the problem is in the fwrite() nd fread() lines.
Map[][] is an array of ints. That''s 4 bytes for each element. However, your only reading and writing 1 byte for each element, so only 1/4 of the array is being written. change the lines to the following and it should work fine.

fwrite(Map,sizeof(int)*(MAP_ARRAY_X*MAP_ARRAY_Y),1,fptr);
fread(Map,sizeof(int)*(MAP_ARRAY_X*MAP_ARRAY_Y),1,fptr);


JS
http://www.thepeel.com/void
JShttp://www.thepeel.com/void

This topic is closed to new replies.

Advertisement