Question about save files

Started by
27 comments, last by nentwined 15 years, 11 months ago
I've been working on an RPG on and off for a while, and I'd been having difficulties with saving and loading. I'd been using some odd procedure, but switched over to a standard 'fout', 'fin' system. I ran into trouble, though, because pieces of data such as "short sword" that contain more than one word that are assigned to one variable (in this case, Player1.weapon). I was using strings to store the variables, and when I went to load my game, instead of 'Player1.weapon="Short Sword"' the game would recognize the space as a break and set 'Player1.weapon="Short"' and 'Player1.armor="sword"'. The tutorial on saving and loading that I was using said that changing values to character arrays would make it so that I could tell the program to input the save values as a line instead of switching to the next value at the end of each word in the save file, so I switched all my strings out for char*. Saving functions exactly the same (I've checked my save files to ensure that the change in variable type wasn't changing the data storage) however when I go to load, it crashes before it even starts loading the first value. Here is some of the save code:

fout << Player1.name;
fout << "\n" << Player2.name;
fout << "\n" << Player3.name;
fout << "\n" << Player4.name;
fout << "\n" << Player1.exp;
fout << "\n" << Player2.exp;
fout << "\n" << Player3.exp;
fout << "\n" << Player4.exp;

etc. and the start of the loading code:

void load()
{
cout << "enter the name of the file you wish to load.\n";
cin >> filename;
ifstream fin(filename);
//usually crashes somewhere around here
if (!fin)
	{
	cout << "that file doesn't exist.\n";
	load();
	}
else if (fin)
	{
	fin >> Player1.name;
	fin >> Player2.name;
	fin >> Player3.name;
	fin >> Player4.name;
	fin >> Player1.exp;
	fin >> Player2.exp;
	fin >> Player3.exp;
	fin >> Player4.exp;

etc. filename is a char[17] Anyone have any advice on this?
Advertisement
Have you considered using an XML-based system? TinyXml is simple and easy to use for such purposes.
you can use a different character than whitespace to seperate your strings.
Of course you should choose a character that does not occur in your strings ;) ( '|' perhaps)


fout << string1;
fout << '|';
fout << string2;
[\code]

for reading you can use the getline function:
char buffer[50];fin >> getline(buffer,50,'|');string1=buffer;fin >> getline(buffer,50,'|');string2=buffer;


btw: usually forums have some buttons to place tags - whats the code tag :?
Quote:Original post by MJP
Have you considered using an XML-based system? TinyXml is simple and easy to use for such purposes.


I have over 14000 lines of code in C++, I'm not about to switch to XML because I ran into one bug. Thanks for the suggestion, though.

Quote:original post by Lastmerlin
you can use a different character than whitespace to seperate your strings.
Of course you should choose a character that does not occur in your strings ;) ( '|' perhaps)


I saw an example of code where a coma was used to separate the lines, but I already am saving well over 300 pieces of data, and that will probably go up to well over 1000 by the time I'm done, so without line breaks the save files will be really difficult to read if I want to go in and test out things later, or if there's some sort of bug in saving that I don't know about. Plus, that would mean 1000 more lines of code to input the break character and a lot of additional code in the loading section. Thanks for the suggestion, though, it should work if I can't find an easier way.
Quote:
I have over 14000 lines of code in C++, I'm not about to switch to XML because I ran into one bug. Thanks for the suggestion, though.


He meant use TinyXML for your saving/loading purposes, not your entire game (which isn't even possible).

void load(){cout << "enter the name of the file you wish to load.\n";cin >> filename;ifstream fin(filename);...


You've already declared the variable filename, right?

ifstream fin(filename);//usually crashes somewhere around hereif (!fin)	{	cout << "that file doesn't exist.\n";	load();	}else if (fin)...


You should probably handle this differently, perhaps using the ifstream's is_open() function. Like this:
ifstream fin(filename);std::string line;if(fin.is_open()){ while(!fin.eof()){  std::getline(fin, line)  // Or however you want to do this } fin.close();}else { // File didn't open, report error or do something else}
Quote:Original post by Shakedown
Be sure to .close() your files when you're finished writing/reading to them

Meh. The ifstream destructor will take care of that.
Quote:Original post by SiCrane
Quote:Original post by Shakedown
Be sure to .close() your files when you're finished writing/reading to them

Meh. The ifstream destructor will take care of that.


True.
Quote:Original post by Shakedown
Quote:
I have over 14000 lines of code in C++, I'm not about to switch to XML because I ran into one bug. Thanks for the suggestion, though.


He meant use TinyXML for your saving/loading purposes, not your entire game (which isn't even possible).


Oh, I've heard of XML as a language, but I was assuming that I couldn't mix and match C++ and XML. I have decent knowledge of C++ and Basic (enough to get me in trouble, at least ^_^) but I don't know a lot about other languages.


Quote:Original post by Shakedown
You should probably handle this differently, perhaps using the ifstream's is_open() function.


I used that code, since it seems to handle things in a neater fashion, but it's still causing problems. I switched some variables around so that I began the load process with int values and then went to the char* values. It seemed to handle the ints fine, but when I got to the char*s, it crashed as always, giving me the following error: "unhandled exception in Song.exe (MSVCP60D.dll): 0xC0000005: Access violation"
Quote:Original post by DarkAnima
Quote:Original post by Shakedown
Quote:
I have over 14000 lines of code in C++, I'm not about to switch to XML because I ran into one bug. Thanks for the suggestion, though.


He meant use TinyXML for your saving/loading purposes, not your entire game (which isn't even possible).


Oh, I've heard of XML as a language, but I was assuming that I couldn't mix and match C++ and XML. I have decent knowledge of C++ and Basic (enough to get me in trouble, at least ^_^) but I don't know a lot about other languages.


Quote:Original post by Shakedown
You should probably handle this differently, perhaps using the ifstream's is_open() function.


I used that code, since it seems to handle things in a neater fashion, but it's still causing problems. I switched some variables around so that I began the load process with int values and then went to the char* values. It seemed to handle the ints fine, but when I got to the char*s, it crashed as always, giving me the following error: "unhandled exception in Song.exe (MSVCP60D.dll): 0xC0000005: Access violation"


XML is a way of saving data in a readable way. it's a markup language very similar to HTML.
Quote:Original post by SiCrane
Quote:Original post by Shakedown
Be sure to .close() your files when you're finished writing/reading to them

Meh. The ifstream destructor will take care of that.


It ain't C++ for no reason!

This topic is closed to new replies.

Advertisement