Page 1 of 1

File reading/writing problem (map editor)

Posted: Wed Jan 26, 2011 4:41 pm
by superLED
Hey there, people!
I am preparing myself for the Global Game Jam. I am working on a map editor for my game engine, but some problems can't be solved with my own fingers.

When I am drawing stuff on the screen (with my mouse), if I draw like a sign on one place, and later draw a sign at the same place, then I won't write that down in my map file again.
So, I am searching for this text "sign:x,y" (which is part of a comment), and if that's already written, I wont go further with my code.
But somehow, it can never find what I am searching for, so it's constantly writing the same shit over and over.

Code: Select all

if(leftClick == true)
{
	if(tile == "sign")
	{
		// Read
		ifstream myFileRead;
		myFileRead.open("mainMap.txt");

		string strFile;

		myFileRead >> strFile; // put the file content inside a string

		myFileRead.close();
		
		// Converting the x and y position to a string
		ostringstream ostr;
		ostr << x;
		string str_x = ostr.str();
		ostringstream ostr2;
		ostr2 << y;
		string str_y = ostr2.str();

		string pos = "sign:" + str_x + "," + str_y;  // This is the text we should be looking for
		size_t found = strFile.find(pos);  // Will be used to check if "pos" has already been written

		if(found == string::npos)  // if "pos" is not found, go on and write down the drawing function
		{
			// Write
			ofstream myFileWrite;
			myFileWrite.open("mainMap.txt", ios_base::app);

			myFileWrite << "sign.draw(" << x <<" - camera.x, " << y << " - camera.y, buffer); // " << pos << endl;  // Writing our drawing function with the new image's position, into our map file

			myFileWrite.close();
		}
	}
}
The text "// sign:0,32" is written as intended, as a comment behind every drawing function. But when I use strFile.find(pos), it will never find it, so it's passing by the if statement every time.
Is there someone out there who can spot any mistakes?

Re: File reading/writing problem (map editor)

Posted: Wed Jan 26, 2011 5:02 pm
by N64vSNES

Code: Select all

if(leftClick == true)
{
   if(tile == "sign")
   {
      // Read
      ifstream myFileRead;
      myFileRead.open("mainMap.txt");
I had to stop there.

You shoudn't read/write stuff from/to files as stuff is happening.

You should store it in a 2D/3D array and then read/write it all in one go.

So it should be

Code: Select all

if(leftClick == true)
{
   Array[x/32][y/32] = tile;
}
And save it:

Code: Select all

void Save() {
	std::ofstream file("somthing.txt");
	for ( int x = 0;x < width;x++ ) {
		for ( int y = 0;y < height;y++ ) {
			file << Array[x][y];
		}
	}
	file.close();
}
EDIT:
This should solve your problem too becuse it will overwrite the data rather than adding ontop of it.

Re: File reading/writing problem (map editor)

Posted: Wed Jan 26, 2011 9:42 pm
by superLED
Thanks for the quick reply!
I used your example, and modified it a little to better fit my engine.

Works perfect now!

Re: File reading/writing problem (map editor)

Posted: Thu Jan 27, 2011 7:14 am
by N64vSNES
superLED wrote:Thanks for the quick reply!
I used your example, and modified it a little to better fit my engine.

Works perfect now!
Your welcome.