Sign in to follow this  

Saving to file

This topic is 3730 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

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[i][c];
	}

	MapFile.close();

	return true;
}

Share this post


Link to post
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[i][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[i][c];

// if c is odd
if(!(c % 2))
MapFile << " ";
}
}

Share this post


Link to post
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[i][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 this post


Link to post
Share on other sites

This topic is 3730 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this