File I/O problems

Started by
0 comments, last by suikio 20 years, 6 months ago
Well, I''ve come to the point where I have to ask for help. I''m writing a 2d isometric tile game in c++, directx 9. Basically for my map I have a linked list of struct GTILE''s. I have very little experience with file i/o and I can''t seem to get the maps to load and save right. The code compiles fine, and I see the map file being created in my directory, but when I load the file it doesn''t load any of the map information, either that or its not saving the map information. Here are my save and load functions, please let me know if you see something wrong.

void Map::LoadMap(const char* fNAME)
{
	ifstream inMapfile(fNAME, ios::in);
	inMapfile.read(reinterpret_cast(&CurrMapHeader), sizeof(CurrMapHeader));
	//inMapfile.read(reinterpret_cast(&CurrMap), sizeof(CurrMap));

	for (int i = 0; i < 256; ++i)
	{
		for (int h = 0; h < 256; ++h)
		{
			//inMapfile.read(reinterpret_cast(&CurrMap.Loc[h].Gcount), 
			//	             sizeof(CurrMap.Loc[h].Gcount));
			CurrMap.Loc[h].Ghead = NULL;
			GTILE tmp;
			GTILE * head = CurrMap.Loc[h].Ghead;
			//for (short int c = 0; (c < CurrMap.Loc[h].Gcount); ++c)
			//{
				inMapfile.read(reinterpret_cast<char *>(&tmp.Text), 
								sizeof(tmp.Text));
				inMapfile.read(reinterpret_cast<char *>(&tmp.Solid), 
								sizeof(tmp.Solid));
				inMapfile.read(reinterpret_cast<char *>(&tmp.xoff), 
								sizeof(tmp.xoff));
				inMapfile.read(reinterpret_cast<char *>(&tmp.yoff), 
								sizeof(tmp.yoff));
				insert(head, tmp);
			//}
		}
	}
	inMapfile.close();
}

void Map::SaveMap(const char* fNAME)
{
	ofstream outMapfile(fNAME, ios::binary);
	outMapfile.write(reinterpret_cast<const char *>(&CurrMapHeader), sizeof(CurrMapHeader));
	//outMapfile.write(reinterpret_cast<const char *>(&CurrMap), sizeof(CurrMap));
	for (int i = 0; i < 256; ++i)
	{
		for (int h = 0; h < 256; ++h)
		{
			//outMapfile.write(reinterpret_cast<const char *>(&CurrMap.Loc[h].Gcount), 
			//	             sizeof(CurrMap.Loc[h].Gcount));
			for (GTILE* curr = CurrMap.Loc[h].Ghead; curr; curr = curr->next)
			{
				outMapfile.write(reinterpret_cast<const char *>(&curr->Text), 
								sizeof(curr->Text));
				outMapfile.write(reinterpret_cast<const char *>(&curr->Solid), 
								sizeof(curr->Solid));
				outMapfile.write(reinterpret_cast<const char *>(&curr->xoff), 
								sizeof(curr->xoff));
				outMapfile.write(reinterpret_cast<const char *>(&curr->yoff), 
								sizeof(curr->yoff));
			}
		}
	}
	outMapfile.close();
}
</pre>  </i>  </pre> 
Advertisement
Nevermind, I figured it out. I''m an idiot. In my load function I make a GTILE pointer and set it to the head. The problem is that my insert function manipulates the pointer passed to it by reference. All I had to do was skip making a pointer to the head and pass the actual head.

This topic is closed to new replies.

Advertisement