Advertisement

Problem saving/loading map file

Started by May 18, 2000 11:51 PM
0 comments, last by VisualLR 24 years, 7 months ago
for a while now Ive been struggling to save my maps to a file, but Ive ran into a couple of problems that I havent been able to figure out... My map file format is as follows: MAP HEADER ---------- TileSet Filename 1 TileSet Filename 2 . . ---------- MAP DATA the problem I have is with the TileSet filenames.. I can save ''em to the file, no problem, but to be able to read them from the file I needed to have the lengths of each filename, so I "solved" this by putting in an array in the map header that stores the lengths of each filename... the problem is, when I load the header, all the values in the array are 0, but everything else in the header is fine... here''s how it looks more or less: typedef struct { int Width; int Height; int Layers; int TileSets; int *Lengths; } MapHeader; (I also tried using a static array, but the same thing happened). So, when I do: MapHeader hdr; file->Read(&hdr, sizeof(MapHeader)); Width, Height, Layers, Tilesets and all those variables load correctly, but the Lengths array is all zero''d out... any ideas on what to do? (I''ve tried changing the file format several times, but if you have a suggestion on improving the file firmat I''d like to hear it as well). Thanks! Luis Sempe
visual@guate.net
http://www.spheregames.com


You can't read pointers directly from file! My suggestion:

struct MapHeader{  int Width;  int Height;  int Layers;  int TileSets;  int numOfFiles;};struct TileFile{  int length;  char *name;   // can't be used to read from file directly};void someFunction(){  MapHeader mh;    ...  file->Read (&mh, sizeof(mh));    TileFile *tf = new TileFile[mh.numOfFiles];  for (int j = 0; j < mh.numOfFiles; j++)  {    file->Read (&tf[j].length, sizeof(tf[j].length));    tf[j].name = new char[tf[j].length+1];    file->Read (tf[j].name, tf[j].length);    tf[j].name[tf[j].length] = 0;  }  ...  // remember to delete[]} 


Might have some error here... you got the point?

Edited by - DerekSaw on May 19, 2000 1:42:04 AM
"after many years of singularity, i'm still searching on the event horizon"

This topic is closed to new replies.

Advertisement