This topic is 4395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello. Im currently in the process of trying my first attempt at saving/loading data in our map editor (my first :)). The problem is when I load my map, or atleast attempt to, it doesent for some reason. Saving functions properly, heres its definition:
void Map::Save(std::string filename,int maparray[25][25], int mapevent[25][25])
{
std::string saveto = "data/Maps/"+filename+".txt";
ofstream myfile;
myfile.open(saveto.c_str(), ios::out);
for(int y = 0;y < 25; y++)
{
for(int x = 0;x < 25; x++)
{
myfile << maparray[x][y];
}
}

for(int u = 0;u < 25; u++)
{
for(int c = 0;c < 25; c++)
{
myfile << mapevent[c][u];
}
}

myfile.close();
}


Now when I try loading the array it returns all the values as zero, Heres is its definition:
void Map::Load(std::string filename,int maparray[25][25], int mapevent[25][25])
{
ifstream myfile;
for(int y = 0;y < 25; y++)
{
for(int x = 0;x < 25; x++)
{
myfile >> maparray[x][y];
}
}

for(int u = 0;u < 25; u++)
{
for(int c = 0;c < 25; c++)
{
myfile >> mapevent[c][u];
}
}

myfile.close();
}


Now I've never used ifsteam/ofsteam before so im looking for somewhat of an explination of what to do it. NOTE: the saving works properly, the format looks like such:
Quote:
 22222222222222222222223451111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 1111111111111111111111111111111111111111111111117777777777777777777777789 11111111111111111111111110000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000001111111111111111111111111
Thanks for the help. I appreciate it.

##### Share on other sites
When outputting your file, try putting spaces or other whitespace between the numbers. The formatted extraction operator on istream uses whitespace to delimit the numbers. What's probably happening is that it's trying to read that huge blob as a single number, which overflows and sets the badbit on the stream causing subsequent extractions to fail.

##### Share on other sites
SiCrane, thanks a bunch. +ratings to you my good sir!

##### Share on other sites
Since you have static arrays it seems like it would be much simpler to write each of those as a blob. Considering you are using the very flakey c++ file writing class I would suggest something different. This is C code but should still work.

Also unless you are planning to edit these maps by hand it would probably be wiser to store them in binary format.

// for writing - something like this, of course you need error checking and whatnotFILE *fp = NULL;fp = fopen("data/Maps/"+filename+".txt", "wb+");fwrite(&maparray, 25*25*sizeof(int), 1, fp);fwrite(&mapevent, 25*25*sizeof(int), 1, fp);fclose(fp);// for readingFILE *fp = NULL;int maparray[25][25];int mapevent[25][25];fp = fopen("data/Maps/"+filename+".txt", "rb");fread(&maparray, 25*25*sizeof(int), 1, fp);fread(&mapevent, 25*25*sizeof(int), 1, fp);fclose(fp);

Also I don't know if this is 100% correct I did not check it or run it. It is just a suggestion I am making as I have made many map editors/game content generators of some sort. You should be using binary data unless you want people to easily edit the files. Generally, binary data makes things easier to read/write and less complicated across OSes.

##### Share on other sites
Quote:
 Original post by wyledthe very flakey c++ file writing class

You know not whereof you speak.

You can perform binary I/O with C++ just as well:

// Adapted from original code, with some other minor things being corrected// and my formatting imposed >:)const int mapsize = 25;const int mapdatasize = mapsize * mapsize * sizeof(int);void Map::Save(const std::string& filename,               int maparray[mapsize][mapsize], int mapevent[mapsize][mapsize]) {  // Urg. I'm worried. Why are you taking in arrays for reading/writing?  // Shouldn't they be members of the Map instead?  ofstream(("data/Maps/"+filename+".txt").c_str(), ios::binary | ios::out)  .write(reinterpret_cast<char*>(maparray), mapdatasize)  .write(reinterpret_cast<char*>(mapevent), mapdatasize);  // Automatically .close()d at end of scope, you know.  // Yes, I did the whole thing in one line, just to show off ;)  // It's not too hard to split this up - I'm just illustrating a few  // techniques for this stuff - used *properly* it makes things *cleaner*,  // really!}// Loading is just the same:void Map::Load(const std::string& filename,               int maparray[mapsize][mapsize], int mapevent[mapsize][mapsize]) {  ifstream(("data/Maps/"+filename+".txt").c_str(), ios::binary | ios::in)  .read(reinterpret_cast<char*>(maparray), mapdatasize)  .read(reinterpret_cast<char*>(mapevent), mapdatasize);}

Quote:
 You should be using binary data unless you want people to easily edit the files. Generally, binary data makes things easier to read/write and less complicated across OSes.

Text files may be easily more easily edited, but don't fall into the trap of assuming that binary data is more secure. Also, binary data may be "easier" to read/write (assuming you don't mind things like reinterpret_cast) in some environments, but in C++ any difference is mostly owing to the fact that text formats usually get more fluff inserted into them (i.e. embedded comments or labels on parts of the file, put in so that people can edit it more easily, and which you then have to skip over during parsing). As for binary data being "less complicated across OSes" - I can think of precisely no reasons to favour binary data for cross-platform compatibility, and several reasons to favour text (binary suffers from assumptions about sizeof()s, endian issues, and makes it considerably harder to design for metadata - another sort of 'fluff' - e.g. to mark data that might not be supported on the other platform because the newest version of the program hasn't been ported yet).