Skip to main content
GameDev.net gamedev.net
🔒 Locked

problem loading data

Started by Terminatore3 Aug 3, 2006 at 4:14 PM 5 replies 850+ views
Original Post
Terminatore3
Terminatore3
I have a program where I can enter some data and then save, I can also load the data back into the program if I wish. I am able to save the data then load it in the same session; however, after I leave the program after I save then start it up again and try to load the data it freazes up. here is the code for my functions that load and save my data

	class dataio{
	public:
		void savefile(char * dest, const void * data, int amount); 
		void loadfile(char * source, void * data, int amount); 
	};dataio dataio;

	void dataio::savefile(char * dest, const void * data, int amount)
	{		
	FILE *SaveFile;

	SaveFile = fopen(dest, "wb"); // opens file for writing and in binary mode	

	fwrite (&amount, sizeof(int), 1, SaveFile);

	fwrite (data, sizeof(data), amount, SaveFile);

	fclose (SaveFile);
	}

	void dataio::loadfile(char * source, void * data, int amount)
	{
		FILE *loadfile;

		loadfile = fopen(source,"rb");

		fread(&amount, sizeof(int), 1, loadfile);

		fread (data, sizeof(data), amount, loadfile);

		fclose(loadfile);
	}
and here is the code where I save and load my data


	if(keys[17] && keys['S'])//if control and 's' is pressed
	{
		dataio.savefile("spells.txt",spells,sizeof(spell)*10);//save data to "spells.txt"
	}

	if(keys[17] && keys['L'])//if control and 'l' is pressed
	{
		dataio.loadfile("spells.txt",spells,sizeof(spell)*10);//load data from "spells.txt"

	}
'spell' is a class holding my variables and 'spells' is an array of the class 'spell' I don't think i'm inputting the last parameter in right, but i don't know of any other way. any and all tips are appreciated
GotenRulezU
GotenRulezU
i'm new to this so i'm not positive but i BELIEVE you have to save each member of the spell class individually. not positive tho i'm a noob
<3,
-Goten
Aardvajk
Aardvajk
GotenRulezU is probably right. Unless class spell only contains very simple data types (no strings, no pointers) you can't fread and fwrite it directly like that.

Could you post the code for the spell class?

[EDIT] Irrelevant aside, but why are you calling the file spells.txt when it is binary format?
Terminatore3
Terminatore3
no I can save whole classes this way. Like I said I can save and load in the same session, but when I restart the app it won't let me load the data again.
Terminatore3
Terminatore3
ok, here's the code for the spell class

class spell{public:	float points[10][2];	int amount;	int current;	double sensitivity;};spell spells[10];


EDIT: I named it with the txt extension just because it was the first thing to come in mind, I will change it later so people aren't tempted to change it.
Aardvajk
Aardvajk
[EDIT] You posted that you had solved this while I was typing. Really time for me to go to bed [smile]. Glad it is sorted now.[/EDIT]

Actually, sorry, just noticed:

When you do sizeof(data), at that point all the compiler knows is that data is a void *ptr, so this (I would assume) would be the size of a pointer, not the byte size you seem to require.

Try sizeof(char) or just 1 instead, since you are specifiying the number of bytes in the function parameter.

[EDIT] Edited due to being half asleep and posting nonsense.

Hang on. Right. When you save, you are passing in the size (in bytes) of your data and writing that first to the file, then you are saving the data, but specifiying sizeof(void*),amount as the amount to save. This translates to sizeof(void*)*amount, and since sizeof(void*) is probably 4 with a 32 bit compiler, this is more than you want.

When you load, for no reason you are passing in the size again in bytes through the amount parameter, then immediately overwriting this with the value from the file, then using sizeof(data) again to try to read the data back in.

Replace sizeof(data) with sizeof(char) or 1 in your fread and fwrite calls, then remove the amount parameter from the load function since it is pointless, and make amount a local variable.

HTH
Terminatore3
Terminatore3
ok, easilyconfused gave me an idea and i tinkered around with it for a little bit. where I put sizeof(data) it should have been the size of the class then I put the amount of columns in the array where the amount was.

thanks alot it works perfectly now :p

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.