Saving files with Fstream question

Pages: 12
Is it possible to save global variable values into a save. Then load them back into memory as the same global variables later to save ur current state in a game?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
int global_variable;

void load() {
    ifstream in("save.txt");
    in >> global_variable;
}

void save() {
    ofstream out("save.txt");
    out << global_variable;
}

BTW, don't use global variables.
yeah thats what i was currently testing but the header i forget which one if or of wont load with include im using fstream out that should work fine no?
also what i want to keep track of in the save file is

intergers
bools
strings



for the most part so far
yeah thats what i was currently testing but the header i forget which one if or of wont load with include im using fstream out that should work fine no?

Could you rephrase this? I don't understand what you are asking.

also what i want to keep track of in the save file is

intergers
bools
strings

for the most part so far

What do you mean keep track?
Could you post an example or some code.
What i was saying is that one of the #include <ifstream> or <ofstream> cant be found when i compile. <fstream> loads though and it works for in and out.... so im fine with fstream alone for #include?



Second im keeping track of global variables ints for keeping track of calculations player level etc.... bools for true/false if you have been to a certain place, or if you have an item equipped and so on, and strings for names of different items and playername etc.
Ok here is the full save code up to the point im at now variable wise.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
int SaveGame()
{
ofstream myfile ("Save.txt");
  if (myfile.is_open())
  {
	// Global Player Variables
	myfile << PlayerLevel;
	myfile << PlayerName;
	myfile << PlayerLevel;
	myfile << PlayerMaxLevel;
	myfile << PlayerExperience;
	myfile << PlayerNextExperience;
	myfile << PlayerGold;
	myfile << PlayerHitPoints;
	myfile << PlayerMaxHitPoints;
	myfile << PlayerHitRate;
	myfile << PlayerAttack;
	myfile << PlayerDefense;
	// Global Player Equipment Variables
	myfile << PlayerWeapon;
	myfile << PlayerArmorShield;
	myfile << PlayerArmorBody;
	myfile << PlayerArmorLegs;
	myfile << PlayerArmorHands;
	myfile << PlayerArmorFeet;     
	// Global Player Equipment Strings
	myfile << PlayerWeaponName;
	myfile << PlayerArmorShieldName;
	myfile << PlayerArmorBodyName;
	myfile << PlayerArmorLegsName;
	myfile << PlayerArmorHandsName;
	myfile << PlayerArmorFeetName;
	// Global Player Equipment Description Strings
	myfile << PlayerWeaponDesc;
	myfile << PlayerArmorShieldDesc;
	myfile << PlayerArmorBodyDesc;
	myfile << PlayerArmorLegsDesc;
	myfile << PlayerArmorHandsDesc;
	myfile << PlayerArmorFeetDesc;
	// Global Player Inventory Strings
	myfile << PlayerInventoryItem1;
	myfile << PlayerInventoryItem2;
	myfile << PlayerInventoryItem3;
	myfile << PlayerInventoryItem4;
	myfile << PlayerInventoryItem5;
	myfile << PlayerInventoryItem6;
	myfile << PlayerInventoryItem7;
	myfile << PlayerInventoryItem8;
	myfile << PlayerInventoryItem9;
	myfile << PlayerInventoryItem10;
	// Global Player Inventory Description Strings
	myfile << PlayerInventoryItem1Desc;
	myfile << PlayerInventoryItem2Desc;
	myfile << PlayerInventoryItem3Desc;
	myfile << PlayerInventoryItem4Desc;
	myfile << PlayerInventoryItem5Desc;
	myfile << PlayerInventoryItem6Desc;
	myfile << PlayerInventoryItem7Desc;
	myfile << PlayerInventoryItem8Desc;
	myfile << PlayerInventoryItem9Desc;
	myfile << PlayerInventoryItem10Desc;
	// Global Player Quest Item Strings
	myfile << PlayerQuestItem1;
	myfile << PlayerQuestItem2;
	myfile << PlayerQuestItem3;
	myfile << PlayerQuestItem4;
	myfile << PlayerQuestItem5;
	// Global Player Quest Item Description Strings
	myfile << PlayerQuestItem1Desc; 
	myfile << PlayerQuestItem2Desc;
	myfile << PlayerQuestItem3Desc;
	myfile << PlayerQuestItem4Desc;
	myfile << PlayerQuestItem5Desc;
	// Global Player Equipment Bool Checks
	myfile << CheckPlayerWeapon;
	myfile << CheckPlayerArmorShield;
	myfile << CheckPlayerArmorBody;
	myfile << CheckPlayerArmorLegs;
	myfile << CheckPlayerArmorHands;
	myfile << CheckPlayerArmorFeet;
	// Global Inventory Item Bool Checks
	myfile << CheckPlayerInventoryItem1;
	myfile << CheckPlayerInventoryItem2;
	myfile << CheckPlayerInventoryItem3;
	myfile << CheckPlayerInventoryItem4;
	myfile << CheckPlayerInventoryItem5;
	myfile << CheckPlayerInventoryItem6;
	myfile << CheckPlayerInventoryItem7;
	myfile << CheckPlayerInventoryItem8;
	myfile << CheckPlayerInventoryItem9;
	myfile << CheckPlayerInventoryItem10;
	// Global Player Quest Bool Checks
	myfile << CheckPlayerQuestItem1;
	myfile << CheckPlayerQuestItem2;
	myfile << CheckPlayerQuestItem3;
	myfile << CheckPlayerQuestItem4;
	myfile << CheckPlayerQuestItem5;
	// Misc Global Variables
	myfile << OPT000;
	myfile << OPT001;
	myfile << OPT011;
	myfile << OPT011A;
	myfile << CharacterStatsDisplay;
	myfile << Exit;
	myfile << Test;
	myfile.close();
  }
  else 
  {
	cout << "Unable to open file";
  }
  return 0;
}
Last edited on
Well it saved the file but it saved it as one continuous file how the heck is it going to know where 1 piece of data ends and another starts... how to i make it save each piece of data on the next line?
What i was saying is that one of the #include <ifstream> or <ofstream> cant be found when i compile. <fstream> loads though and it works for in and out.... so im fine with fstream alone for #include?

There are no header files called ifstream or ofstream. ifstream and ofstream classes are both in the fstream header.

Second im keeping track of global variables ints for keeping track of calculations player level etc.... bools for true/false if you have been to a certain place, or if you have an item equipped and so on, and strings for names of different items and playername etc.

I'm still not sure I understand you. You probably have to come up with some sort of format, in which you save your variables to a file, and follow the same format when reading back. For example saving each variable in it's own line, then reading it back in the same order.

EDIT: I just saw your huge code post: You probably have to start using arrays.

how to i make it save each piece of data on the next line?
file << variable << '\n';
Last edited on
Thanks alot. And I have not learned arrays yet im building a fully functional minus grahpics text based console rpg!! This is an awesome learning experience for me and its pretty fun, granted its outdated but its more for the old styleness is what makes it so great.
Last edited on
so for the in

file >> variable >> "\n";

?


Also how would i go about making it so the player can input his own file save name?
Last edited on
No, file >> variable should be enough. It skips whitespaces (so '\n' as well) before reading.
Also how would i go about making it so the player can input his own file save name?
1
2
3
4
std::cout << "Where do you want to save your game?" << std::endl;
std::string filename;
std::getline(std::cin, filename);
std::ofstream out(filename);

EDIT: make sure to not mix formatted(operator >>) and unformatted(getline()) input from the same stream.
Last edited on
I don't understand
std::cout << "Where do you want to save your game?" << std::endl;
std::string filename;
std::getline(std::cin, filename);
std::ofstream out(filename);



im used to using cin >>
and namespace std

1
2
3
4
cout << "What would you like to name your savegame?\n";
string myfile;
cin >> myfile;
ofstream out(myfile);


But i still don't see how to specify string + file extension
Last edited on
I tought you wanted the user to give the full path. In that case :
1
2
3
std::string filename;
std::getline(std::cin, filename);
std::ofstream out("C:/yourpath/" + filename + ".txt");
Last edited on
when the games released im going to have it go to default directory
C:/mygame/


the dir structure is

C:/KingdomOfMythos/Midi/
C:/KingdomOfMythos/Saves/
C:/KingdomOfMythos/HelpFiles/
I'm getting this weird error in compiler on the LoadGame()





error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem *)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'











1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
int LoadGame()
{
ifstream myfile ("Saves/Save.txt");
  if(myfile.is_open())
  {
                //Global Player Variables
	myfile >> PlayerName;
	myfile >> PlayerLevel;
	myfile >> PlayerMaxLevel;
	myfile >> PlayerExperience;
	myfile >> PlayerNextExperience;
	myfile >> PlayerGold;
	myfile >> PlayerHitPoints;
	myfile >> PlayerMaxHitPoints;
	myfile >> PlayerHitRate;
	myfile >> PlayerAttack;
	myfile >> PlayerDefense;
	//Global Player Equipment Variables
	myfile >> PlayerWeapon;
	myfile >> PlayerArmorShield;
	myfile >> PlayerArmorBody;
	myfile >> PlayerArmorLegs;
	myfile >> PlayerArmorHands;
	myfile >> PlayerArmorFeet;
	//Global Player Equipment Strings
	myfile >> PlayerWeaponName;
	myfile >> PlayerArmorShieldName;
	myfile >> PlayerArmorBodyName;
	myfile >> PlayerArmorLegsName;
	myfile >> PlayerArmorHandsName;
	myfile >> PlayerArmorFeetName;
	//Global Player Equipment Description Strings
	myfile >> PlayerWeaponDesc;
	myfile >> PlayerArmorShieldDesc;
	myfile >> PlayerArmorBodyDesc;
	myfile >> PlayerArmorLegsDesc;
	myfile >> PlayerArmorHandsDesc;
	myfile >> PlayerArmorFeetDesc;
	//Global Player Inventory Strings
	myfile >> PlayerInventoryItem1;
	myfile >> PlayerInventoryItem2;
	myfile >> PlayerInventoryItem3;
	myfile >> PlayerInventoryItem4;
	myfile >> PlayerInventoryItem5;
	myfile >> PlayerInventoryItem6;
	myfile >> PlayerInventoryItem7;
	myfile >> PlayerInventoryItem8;
	myfile >> PlayerInventoryItem9;
	myfile >> PlayerInventoryItem10;
	//Global PlayerInventory Description Strings
	myfile >> PlayerInventoryItem1Desc;
	myfile >> PlayerInventoryItem2Desc;
	myfile >> PlayerInventoryItem3Desc;
	myfile >> PlayerInventoryItem4Desc;
	myfile >> PlayerInventoryItem5Desc;
	myfile >> PlayerInventoryItem6Desc;
	myfile >> PlayerInventoryItem7Desc;
	myfile >> PlayerInventoryItem8Desc;
	myfile >> PlayerInventoryItem9Desc;
	myfile >> PlayerInventoryItem10Desc;
	//Global Player Quest Item Strings
	myfile >> PlayerQuestItem1;
	myfile >> PlayerQuestItem2;
	myfile >> PlayerQuestItem3;
	myfile >> PlayerQuestItem4;
	myfile >> PlayerQuestItem5;
	//Global Player Quest Item Description Strings
	myfile >> PlayerQuestItem1Desc;
	myfile >> PlayerQuestItem2Desc;
	myfile >> PlayerQuestItem3Desc;
	myfile >> PlayerQuestItem4Desc;
	myfile >> PlayerQuestItem5Desc;
	//Global Player Equipment Bool Checks
	myfile >> CheckPlayerWeapon;
	myfile >> CheckPlayerArmorShield;
	myfile >> CheckPlayerArmorBody;
	myfile >> CheckPlayerArmorLegs;
	myfile >> CheckPlayerArmorHands;
	myfile >> CheckPlayerArmorFeet;
	//Global Inventory Item Bool Checks
	myfile >> CheckPlayerInventoryItem1;
	myfile >> CheckPlayerInventoryItem2;
	myfile >> CheckPlayerInventoryItem3;
	myfile >> CheckPlayerInventoryItem4;
	myfile >> CheckPlayerInventoryItem5;
	myfile >> CheckPlayerInventoryItem6;
	myfile >> CheckPlayerInventoryItem7;
	myfile >> CheckPlayerInventoryItem8;
	myfile >> CheckPlayerInventoryItem9;
	myfile >> CheckPlayerInventoryItem10;
	//Global Player Quest Bool Checks
	myfile >> CheckPlayerQuestItem1;
	myfile >> CheckPlayerQuestItem2;
	myfile >> CheckPlayerQuestItem3;
	myfile >> CheckPlayerQuestItem4;
	myfile >> CheckPlayerQuestItem5;
	//Misc Global Variables
	myfile >> OPT000;
	myfile >> OPT001;
	myfile >> OPT011;
	myfile >> OPT011A;
	myfile >> CharacterStatsDisplay;
	myfile >> Exit;
	myfile >> Test;
	myfile.close();
}
else
{
	cout >> "Unable to open file";
}
return 0;
}
Last edited on
cout >> "Unable to open file";
should be
cout << "Unable to open file";
doh! i totally missed that i used a text pad to replace all the << to >> to make it fast and i missed that one xD i feel a homer simpson moment coming on!!! nice catch
hey btw i never did figure out the bug to my combat algorithms think you could take a look and see if you could figure it out its 1 tiny little problem, the script works but not exactly as intended
Pages: 12