Saving files with Fstream question

Pages: 12
If you post it, I can take a look.
one second, the code you gave me for naming the file is still bugged


c:\users\snaef98\documents\visual studio 2008\projects\kingdom of mythos revised\kingdom of mythos revised\kingdom of mythos revised.cpp(1346) : error C2664: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const char *'


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
114
115
116
int SaveGame()
{
	string filename;
	cout << "Please enter a name for your saved game! <a-z>";
	cin >> filename;
	ofstream myfile ("C:/KingdomOfMythos/Saves/" + filename + ".txt");
if (myfile.is_open())
  {
	// Global Player Variables
	myfile << PlayerLevel << "\n";
	myfile << PlayerName << "\n";
	myfile << PlayerLevel << "\n";
	myfile << PlayerMaxLevel << "\n";
	myfile << PlayerExperience << "\n";
	myfile << PlayerNextExperience << "\n";
	myfile << PlayerGold << "\n";
	myfile << PlayerHitPoints << "\n";
	myfile << PlayerMaxHitPoints << "\n";
	myfile << PlayerHitRate << "\n";
	myfile << PlayerAttack << "\n";
	myfile << PlayerDefense << "\n";
	// Global Player Equipment Variables
	myfile << PlayerWeapon << "\n";
	myfile << PlayerArmorShield << "\n";
	myfile << PlayerArmorBody << "\n";
	myfile << PlayerArmorLegs << "\n";
	myfile << PlayerArmorHands << "\n";
	myfile << PlayerArmorFeet << "\n";     
	// Global Player Equipment Strings
	myfile << PlayerWeaponName << "\n";
	myfile << PlayerArmorShieldName << "\n";
	myfile << PlayerArmorBodyName << "\n";
	myfile << PlayerArmorLegsName << "\n";
	myfile << PlayerArmorHandsName << "\n";
	myfile << PlayerArmorFeetName << "\n";
	// Global Player Equipment Description Strings
	myfile << PlayerWeaponDesc << "\n";
	myfile << PlayerArmorShieldDesc << "\n";
	myfile << PlayerArmorBodyDesc << "\n";
	myfile << PlayerArmorLegsDesc << "\n";
	myfile << PlayerArmorHandsDesc << "\n";
	myfile << PlayerArmorFeetDesc << "\n";
	// Global Player Inventory Strings
	myfile << PlayerInventoryItem1 << "\n";
	myfile << PlayerInventoryItem2 << "\n";
	myfile << PlayerInventoryItem3 << "\n";
	myfile << PlayerInventoryItem4 << "\n";
	myfile << PlayerInventoryItem5 << "\n";
	myfile << PlayerInventoryItem6 << "\n";
	myfile << PlayerInventoryItem7 << "\n";
	myfile << PlayerInventoryItem8 << "\n";
	myfile << PlayerInventoryItem9 << "\n";
	myfile << PlayerInventoryItem10 << "\n";
	// Global Player Inventory Description Strings
	myfile << PlayerInventoryItem1Desc << "\n";
	myfile << PlayerInventoryItem2Desc << "\n";
	myfile << PlayerInventoryItem3Desc << "\n";
	myfile << PlayerInventoryItem4Desc << "\n";
	myfile << PlayerInventoryItem5Desc << "\n";
	myfile << PlayerInventoryItem6Desc << "\n";
	myfile << PlayerInventoryItem7Desc << "\n";
	myfile << PlayerInventoryItem8Desc << "\n";
	myfile << PlayerInventoryItem9Desc << "\n";
	myfile << PlayerInventoryItem10Desc << "\n";
	// Global Player Quest Item Strings
	myfile << PlayerQuestItem1 << "\n";
	myfile << PlayerQuestItem2 << "\n";
	myfile << PlayerQuestItem3 << "\n";
	myfile << PlayerQuestItem4 << "\n";
	myfile << PlayerQuestItem5 << "\n";
	// Global Player Quest Item Description Strings
	myfile << PlayerQuestItem1Desc << "\n"; 
	myfile << PlayerQuestItem2Desc << "\n";
	myfile << PlayerQuestItem3Desc << "\n";
	myfile << PlayerQuestItem4Desc << "\n";
	myfile << PlayerQuestItem5Desc << "\n";
	// Global Player Equipment Bool Checks
	myfile << CheckPlayerWeapon << "\n";
	myfile << CheckPlayerArmorShield << "\n";
	myfile << CheckPlayerArmorBody << "\n";
	myfile << CheckPlayerArmorLegs << "\n";
	myfile << CheckPlayerArmorHands << "\n";
	myfile << CheckPlayerArmorFeet << "\n";
	// Global Inventory Item Bool Checks
	myfile << CheckPlayerInventoryItem1 << "\n";
	myfile << CheckPlayerInventoryItem2 << "\n";
	myfile << CheckPlayerInventoryItem3 << "\n";
	myfile << CheckPlayerInventoryItem4 << "\n";
	myfile << CheckPlayerInventoryItem5 << "\n";
	myfile << CheckPlayerInventoryItem6 << "\n";
	myfile << CheckPlayerInventoryItem7 << "\n";
	myfile << CheckPlayerInventoryItem8 << "\n";
	myfile << CheckPlayerInventoryItem9 << "\n";
	myfile << CheckPlayerInventoryItem10 << "\n";
	// Global Player Quest Bool Checks
	myfile << CheckPlayerQuestItem1 << "\n";
	myfile << CheckPlayerQuestItem2 << "\n";
	myfile << CheckPlayerQuestItem3 << "\n";
	myfile << CheckPlayerQuestItem4 << "\n";
	myfile << CheckPlayerQuestItem5 << "\n";
	// Misc Global Variables
	myfile << OPT000 << "\n";
	myfile << OPT001 << "\n";
	myfile << OPT011 << "\n";
	myfile << OPT011A << "\n";
	myfile << CharacterStatsDisplay << "\n";
	myfile << Exit << "\n";
	myfile << Test << "\n";
	myfile.close();
  }
  else 
  {
	  cout << "Unable to open file";
  }
  return 0;
}
ofstream myfile (std::string("C:/KingdomOfMythos/Saves/" + filename + ".txt").c_str());
Next time please specify which line caused the error. It can be very hard without knowing it.
Well the save and load sorta worked, the save works, the load however messes up the strings because some of them are descriptions for items or names of stuff, IE it has spaces like

Squire's Sword

or

Worn by citizens of Mythos.

etc.....
This is how I would:
SaveGame() function could remain the same, but the LoadGame() would look a bit different:

1
2
3
4
5
6
7
8
9
10
void LoadGame() {
     ifstream in("savegame.txt");
     string line;
     getline(in, line); //This reads a whole line into varible 'line';
     //The next step depends on what is the type of the variable you want the contents of 'line' to be saved
    //if it's a string then it's simple : string_variable = line;
    //if it's a number then you have to convert 'line' to for example int. To how to do this read this article and pick the method you like best : http://cplusplus.com/articles/numb_to_text/ 

   //Do the above method for every variable. Again I recommend to use arrays, it will make your code much much sorter and managable.   
}


Alternatively you can make short functions like these :
1
2
3
4
5
6
7
8
9
10
11
string LoadString(ifstream& in) {
    string res;
    getline(in, res);
    return res;
}
int LoadInt(ifstream& in) {
    string line;
    getline(in, line);
    return boost::lexical_cast<int>(line); //See the above link, if you haven't downloaded boost yet (highly recommended!) and want to use some other method
 
}

and then use them like this:
1
2
3
4
5
6
void LoadGame() {
    ifstream in("savegame.txt");
    string_variable = LoadString(in);
    int_variable = LoadInt(in);
    //...
}


Again : don't mix getline() with operator>> on the same stream, choose either one and stick to it. It is possible to mix them, but it's error prone, and makes your life much harder.

For now im going to stick with the system you helped me design. Im just making sure users input _ for space on anything with strings which for the time being is only PlayerName which they input only once... the rest is all pre-programmed im using _ on all other strings instead of spaces and its worked fine. If other problems arise ill call on you but im gonna mark this topic as solved for now. i appreciate your help.

For now im going to stick with the system you helped me design. Im just making sure users input _ for space on anything with strings which for the time being is only PlayerName which they input only once... the rest is all pre-programmed im using _ on all other strings instead of spaces and its worked fine. If other problems arise ill call on you but im gonna mark this topic as solved for now. i appreciate your help.


What you want is some object serialization and de-serialization mechanism. We have that in Standard Java. Despite C++ STL advancement, it still lacks behind Java in terms of API rich-ness and varieties. I can forsee this to be one of them.

So I believe different C++ developers build their "own" version for write and read objects from file. For performance I would think write/read in binary format would be better than write/read in text format. Making this part of C++ STL would be great in future. Purely my opinion though.
http://www.boost.org/doc/libs/1_44_0/libs/serialization/doc/index.html


I notice about Boost from this forum and take a look. It seem to be C++ add-on for those classes that are missing from the Standard C++ STL. I wonder it seem some Boost libraries are so useful that most developers would want them but somehow the C++ committee is a bit slow in approving them into the C++ standard.

For Java, the process is much shorter. Once certain classes are deemed so useful, the next JDK release would make them as part of the core. The time taken to approve as standard for Java is much faster.

Maybe this is my complaint but C++ committee should work faster to incorporate Boost libraries into Standard C++.
Topic archived. No new replies allowed.
Pages: 12