Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

suikio

File I/O problems

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

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[i][h].Gcount));
			CurrMap.Loc[i][h].Ghead = NULL;
			GTILE tmp;
			GTILE * head = CurrMap.Loc[i][h].Ghead;
			//for (short int c = 0; (c < CurrMap.Loc[i][h].Gcount); ++c)
			//{
				inMapfile.read(reinterpret_cast(&tmp.Text), 
								sizeof(tmp.Text));
				inMapfile.read(reinterpret_cast(&tmp.Solid), 
								sizeof(tmp.Solid));
				inMapfile.read(reinterpret_cast(&tmp.xoff), 
								sizeof(tmp.xoff));
				inMapfile.read(reinterpret_cast(&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(&CurrMapHeader), sizeof(CurrMapHeader));
	//outMapfile.write(reinterpret_cast(&CurrMap), sizeof(CurrMap));
	for (int i = 0; i < 256; ++i)
	{
		for (int h = 0; h < 256; ++h)
		{
			//outMapfile.write(reinterpret_cast(&CurrMap.Loc[i][h].Gcount), 
			//	             sizeof(CurrMap.Loc[i][h].Gcount));
			for (GTILE* curr = CurrMap.Loc[i][h].Ghead; curr; curr = curr->next)
			{
				outMapfile.write(reinterpret_cast(&curr->Text), 
								sizeof(curr->Text));
				outMapfile.write(reinterpret_cast(&curr->Solid), 
								sizeof(curr->Solid));
				outMapfile.write(reinterpret_cast(&curr->xoff), 
								sizeof(curr->xoff));
				outMapfile.write(reinterpret_cast(&curr->yoff), 
								sizeof(curr->yoff));
			}
		}
	}
	outMapfile.close();
}
[/CODE]    

Share this post


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

Share this post


Link to post
Share on other sites

  • 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!