# [C++] Save and load system

This topic is 3700 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Can somebody help me, it'l save but not load!
void save()
{
ofstream out("SAVE.txt");

if (!out) {
cout << "[ERROR]" << endl;
cin.get();
}

out << sword << shield  << cuirass << greaves;
out << gold << boots << game_running << sword_skill;
out << axe_skill << blunt_skill << room << player_stats[0];
out << player_stats[2] << player_stats[3] << player_stats[4];
out << player_race << player_name;
out.close();				//close it
cout << "Game Saved" << endl << endl;
}

{
ifstream in("SAVE.txt");

if (!in) {
cout << "[ERROR]" << endl;
cin.get();
}

in >> sword >> shield  >> cuirass >> greaves >> gold >> boots >> game_running >> sword_skill >> axe_skill >> blunt_skill >> room >> player_stats[0] >> player_stats[1] >> player_stats[2] >> player_stats[3] >> player_race >> player_name;
in.close();
cout << "Game Loaded" << endl << endl;
}


##### Share on other sites
Maybe I'm just not seeing it, but let's see how this is called. Seems like the problem would be in the function itself, but let's just see it.

Also, what bug testing have you done? Have you discovered anything that might help our diagnosis?

Have you checked that the file was saved, or are you taking your program's word for it?

##### Share on other sites
Post what is acctually being written to the file.

If I were to guess though you are writing integers and strings to the file and probobly without spaces inbetween. When you try to read it back in it does not know what to do because it is a big mess of numbers and characters.

##### Share on other sites
Here's my shoe size, my waist size, and the amount of change in my pocket (in cents): 1131138. Easy to write, hard to read.

##### Share on other sites
It is called using
if (command == "save")
save();

I have checked that it does create a file, and it does look like Sneftel's post:
00002001000581030HumanGary

##### Share on other sites
Bingo! Spaces did it:
0 0 0 0 20 0 1 0 0 0 5 8 10 3 0 Human Gary

:)