• Advertisement

Archived

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

fwrite & fread problem. What could cause it?

This topic is 6009 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 have a problem. Sometimes when I save a map and load it it''s all screwed up. Here are my functions:
  
typedef struct  
{  
	unsigned char Look;
	unsigned char Feature;
}DataSquare;

Map::AllocateMemory()
{
	mData=new DataSquare[mWidth*mHeight];
	Reset();
}	 

Map::Reset()
{
	for(int i=0;i<mWidth*mHeight;i++){mData[i].Feature=0;mData[i].Look=0;}
}


Map::SaveMap(char *name)
{
	mFile=fopen(name,"wt");
	fprintf(mFile,"Name: %s\n",mName);
	fprintf(mFile,"Width: %i\n",mWidth);
	fprintf(mFile,"Height: %i\n",mHeight);
	fwrite(mData,sizeof(DataSquare)*(mWidth*mHeight),1,mFile);
	fclose(mFile);
}

Map::LoadMap(char *name)
{
	
	mFile=fopen(name,"rt");
	fscanf(mFile,"Name: %s\n",mName);
	fscanf(mFile,"Width: %i\n",&mWidth);
	fscanf(mFile,"Height: %i\n",&mHeight);
	AllocateMemory();
	fread(mData,sizeof(DataSquare)*(mWidth*mHeight),1,mFile);
	

	fclose(mFile);
}
  
Something causes all the tiles to move one to the left and change. Like if some bytes/bits are wrong at the beginning. Please help! /MindWipe "If it doesn''t fit, force it; if it breaks, it needed replacement anyway."

Share this post


Link to post
Share on other sites
Advertisement
I tried it some times. And it seems like when I save the look & feature get''s mixed up. But how can this be? And what can I do to fix it?

/MindWipe



"If it doesn''t fit, force it; if it breaks, it needed replacement anyway."

Share this post


Link to post
Share on other sites
Looks like you are mixing a Text Read and a Binary read in the same file. I would say investigate that first... If you want totally text output/input, try using a comma or tab delimitor.

-JT

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
this sounds like a big endian-little endian problem.
are you transfering the data file to a different machine?



Share this post


Link to post
Share on other sites
I changed it to binary only. And now it seems to be working!

Thanks alot!

If I get this game sold I will surely donate money to GameDev.net

/MindWipe

"If it doesn''t fit, force it; if it breaks, it needed replacement anyway."

Share this post


Link to post
Share on other sites

  • Advertisement