Jump to content
  • Advertisement
Sign in to follow this  
ShoeStringGames

Saving/Loading an array problems in c++

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

If you intended to correct an error in the post then please contact us.

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];
				}
			}

	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])
{
	std::string loadfrom = "data/Maps/"+filename+".txt";
	ifstream myfile;
	myfile.open(loadfrom.c_str(), ios::in);
	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];
				}
			}

	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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 whatnot
FILE *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 reading
FILE *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 this post


Link to post
Share on other sites
Quote:
Original post by wyled
the 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).

You should all read this.

Share this post


Link to post
Share on other sites
The point about using binary data was that you don't have to assume sizeof's and you can specify your own bytes for writing/reading. Yes endian-ness is an issue but that can be solved at read time depending on your OS/hardware. It DOES make things less complicated in my own experience (writing game software across PC, linux, and mac os x), however you may find things differently. What I wrote was a suggestion it was not a demand. However I do not appreciate you downing my suggestions because they were not incorrect. I didn't use sniplets I just used code off the top of my head to help a guy out who is obviously learning. Your suggestions are great too but there was no reason to say that all of mine were incorrect, because they weren't.

My reason for saying the if/of class is flakey is that any writing class (at that level) seems absurd to me since the original functions made in C work just as well to perform the same thing. Its a re-creation that's useless and only adds complexity to an issue that should be very simple.

My answers were the simple solution to a person who probably doesn't want a complicated explaination at this point. However if you would like to debate I would be more than happy to. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!