• 13
• 15
• 19
• 27
• 9

Saving to file

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

Recommended Posts

I successfully load from a txt file which contains this kind of data: 01 00 00 00 07 00 10 01 01 09 ... etc. But when I save to the txt file, it saves it wrong, like this: 01000000070010010109 ... etc What I need to edit here to save it correctly?
int Tiles[TILES_Y][TILES_X];

bool CTileMap::Save(std::string filename)
{
int i, c;

std::ofstream MapFile;
MapFile.open(filename.c_str());

for(i=0;i<TILES_Y;i++)
{
for(c=0;c<TILES_X;c++)
MapFile << Tiles[c];
}

MapFile.close();

return true;
}



Share on other sites
Looks like you just need to store a space between elements.

for(i=0;i<TILES_Y;i++){	for(c=0;c<TILES_X;c++)		MapFile << Tiles[c] << " ";}

Edit: Oops, you need to do it after every other character:

for(i=0;i<TILES_Y;i++){	for(c=0;c<TILES_X;c++)	{		MapFile << Tiles[c];				// if c is odd		if(!(c % 2))			MapFile << " ";	}}

Share on other sites
I haven't wrote c++ in quite some time but try this;
bool CTileMap::Save(std::string filename){	int i, c;	std::ofstream MapFile;	MapFile.open(filename.c_str());	for(i=0;i<TILES_Y;i++)	{		for(c=0;c<TILES_X;c++)                {			MapFile << Tiles[c];                    //You need to put the space character in here                        MapFile << ' ';                     }	}	MapFile.close();	return true;}

Again I am not positive this will work since its been such a long time but that is the idea of what you need to do, you need to add a blank space character after it reads/prints the array.

Hope it helps;

XXChester

Share on other sites
I got it working good with this:
	for(i=0;i<TILES_Y;i++)	{		for(c=0;c<TILES_X;c++)                {			MapFile << Tiles[c];                        MapFile << " ";                 }	}

Thanks for help :)