Jump to content
  • Advertisement
Sign in to follow this  
Nokturnal

File I/O

This topic is 4321 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 written a game in which i am storing the data in a file in binary mode.Basically its high score data.There is no problem in storing / retriving the data.But if the user goes & tinkers with the saved file, this ends up loading corrupted data followed by a crash.Is there any way to avoid loading data from a corrupted file. Cheers! Dummy code:-


#include <fstream>

using namespace std;


struct FileData
{
	int rec;
	string name[3];
	int score[3];
};

int main()
{
	FileData read;
	ofstream data("my_data.txt",ios_base::binary);
	FileData me;

	me.name[0]="aa";
	me.rec=2;
	me.name[1]="bb";
	me.name[2]="cc";
	me.score[0]=1;
	me.score[1]=2;
	me.score[2]=3;

	data.write((char*)&me,sizeof(FileData));
	data.close();


	ifstream load("my_data.txt",ios_base::binary);
	load.read((char*)&read,sizeof(FileData));

	cout<<read.rec;
	
	for(int i=0;i<read.rec;i++)
	{
		cout<<read.name<<" "<<read.score<<endl;
	}



	return 0;
}


Share this post


Link to post
Share on other sites
Advertisement
If the user is dumb enough to tinker with a binary file - you can't really prevent that. A work around would be store your high score information on a server. The app merely queries the server for the latest high score info, and updates when a new high score is achieved. In this manner, the user doesn't have access to any data, so he cannot corrupt it.

You could also enclose your file parsing functions in try/catch blocks and handle the exception. How you handle it is up to you - erase high scores, create some default high scores, etc.

Share this post


Link to post
Share on other sites
I find it unlikely that someone who doesn't know what they are doing will mess with a binary file. I suppose on the chance they do, have something to recreate a new file if you detect that it is corrupted. They may lose their scores, but hey it is their own fault for not backing it up before tinkering.

Edit: To answer your question. I dunno much about that sort of thing to prevent corrupt files from being loaded. But I imagine you could store a hash of some sorts in another file, and if they don't match up then something isn't right. Therefor don't continue reading the file.

Share this post


Link to post
Share on other sites
Thanks for the replies guys,i guess storing hash or retriving scores from the server will be an overkill for what i am developing.I guess the best bet would be to catch the exception & recreate &&& load the data from there.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!