Question about save files

Started by
27 comments, last by nentwined 15 years, 11 months ago
Quote:Original post by DarkAnima
The suggestion was made to remove the spaces, but that would involve creating new variable sets for storing the values without spaces and then turning those into values with spaces after loading, which would take a long time and a lot of code.


What?

string s; // So our routine doesn't affect states = Player1.name;replace(s.begin(),s.end(),' ','_');fout << s << endl;s = Player2.name;replace(s.begin(),s.end(),' ','_');fout << s << endl;s = Player3.name;replace(s.begin(),s.end(),' ','_');fout << s << endl;// etc


// No need for intermediates herefin >> Player1.name;replace(Player1.name.begin(),Player1,name.end(),'_',' ');fin >> Player2.name;replace(Player2.name.begin(),Player2,name.end(),'_',' ');fin >> Player3.name;replace(Player3.name.begin(),Player3,name.end(),'_',' ');// etc

We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
Advertisement
Quote:Original post by DarkAnima
if (choice=!1) goto donesave;	}	fout << Player1.name;	fout << "\n" << Player2.name;	fout << "\n" << Player3.name;       //etc      	fout << "\n" << web.owned;	fout << flush;	fout.close();donesave:	;



Oh no you didn't!
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
Oh, I like that, Erissian! I hadn't thought of that. It will add a lot to my code (I don't know exactly how many char*/string values there are, but it's at least 72... Plus, unless my players decide to be clever and give characters names with spaces, the names themselves will be one word) but it's a very functional idea.
Quote:Original post by erissian
Quote:Original post by DarkAnima
if (choice=!1) goto donesave;	}	fout << Player1.name;	fout << "\n" << Player2.name;	fout << "\n" << Player3.name;       //etc      	fout << "\n" << web.owned;	fout << flush;	fout.close();donesave:	;



Oh no you didn't!


lol, goto gets a lot of criticism. I guess since I'm so used to it from my years programming in basic, I still use it now and then in my code when I'm trying to think of quick, sloppy ways of fixing potential problems (in this case, if I call the save routine again if the player says that they don't want to save, once the player exits, it will go back to where it left off in the save routine and save without asking the player). I'll eventually do something similar to what's going on in the load section now, but goto is working at the moment.
Quote:Original post by DarkAnima
Another thing I was looking into and just thought of again, was getting definitions for the monsters and items put into a header file or something I can #include at the start of the main game file so that I can keep my code separate from everything else. I tried doing it with my monsters, but at first it told me that I hadn't defined the variables, and when I returned the variable initiation to my code (just the definition of the monster class, really) it told me that I was redefining the variables. After spending a couple hours trying to figure out what was going wrong, I gave up, but since there's going to be ~300 items and ~150 monsters I really would like to store all that data away from the program to keep things less cluttered. I'm sure I'm just forgetting something silly, or my inexperience is showing.


I solved this problem. I was including my definition files in the workspace in Microsoft Visual C++, so it was putting all those values into the project, then trying to #include them, which is why it told me that I had already defined them. I'm happy that I got that working, it really unclutters and simplifies a lot of code.

I still would like to know why the cin function is unhappy with char*. I have plenty of good workarounds, so if I can't figure out how to make cin and char* be friends, I'll go back to strings and use one of the workarounds, but I'd rather fix the problem than find a complicated way around it, if possible.

[Edited by - DarkAnima on May 8, 2008 11:16:57 PM]
Quote:Original post by DarkAnimaI still would like to know why the cin function is unhappy with char*. I have plenty of good workarounds, so if I can't figure out how to make cin and char* be friends, I'll go back to strings and use one of the workarounds, but I'd rather fix the problem than find a complicated way around it, if possible.

I took a peek at the other code you posted, and the char* in the character class triggers me. I can't give you more than an educated guess because I never work with ifstreams and ostreams in C++. I think you failed to allocate (enough) space for the reading the string.

char* text = NULL;fin >> text; // will crash, fin does not allocate space for your array



This can also go wrong:

char* text = "bob"; // allocated 4 chars ("bob" + null terminator)// the file contains "bobIsAJollyGoodFellow"fin >> text; // will crash, "bob" will fit, "IsAJollyGoodFellow" overflows the 4 chars allocated


Again, and educated guess: std::string will not have this problem because it does it's own memory allocation.

The solution, if you want to keep using char*, is to allocate a large enough buffer.

char buffer[1024];char* text;cin >> buffer; // will work as long as the line is not longer than 1024 charactersint len = strlen(buffer);text = new char[len + 1]; // + null terminatorstrcpy(text, buffer);



Again, I could be totally wrong here as I'm basing this on an educated guess because I never work with ifstream and ostream.
STOP THE PLANET!! I WANT TO GET OFF!!
Quote:Original post by Structural
Quote:Original post by DarkAnimaI still would like to know why the cin function is unhappy with char*. I have plenty of good workarounds, so if I can't figure out how to make cin and char* be friends, I'll go back to strings and use one of the workarounds, but I'd rather fix the problem than find a complicated way around it, if possible.

I took a peek at the other code you posted, and the char* in the character class triggers me. I can't give you more than an educated guess because I never work with ifstreams and ostreams in C++. I think you failed to allocate (enough) space for the reading the string.


YOU'VE GOT IT!!! Yes! That's what's wrong, and I never thought of it. The program is fine when it's setting the variables normally, but it's not sure how to deal with char values as it loads them because of the space allocation. I'm going to see if I can use something besides char*, perhaps char[33] or something.

The only problem with setting lengths for the character arrays is that the lengths are different, and I always get errors saying that it can't convert from char[x] to char[y]. I know it's supposed to be able to adjust and have the lengths set at the end of variables, but I'm not sure how to fix this, since I'm not used to using character arrays. I'm good with strings, though. Once my friends and I finish watching 'Tin Man' I think I'll go back to strings and use one of the suggested fixer characters so that it can read them in. I'll post my success/failure in a few hours ^_^. Thanks for all your help and advice. I will try to return the favor in the future.
Okay, so I went into my program and used the code that Erissian suggested, so now when the game saves weapon data, it puts an underscore where the spaces are and removes it when it loads. I just have to hope I don't have a player who wants a character with two names ^_^. I went into the save file I made a while back (I don't feel like starting my test of the game from the beginning again) and changed all the spaces to underscores manually. I'm going to load it up and see if we have success...

Saving worked perfectly, but it freezes if I try to load. Debugging...

It kept freezing at the end of the load sequence because it wasn't realizing I'd reached the end of the file (the load sequence was accomplished through a while loop that waited for eof, but it apparently never realized that it reached eof). I modified Shakedown's awesome code (thank you again, for that ^_^) and changed the while loop to go until the file closed, then closed the file at the end of the load sequence. It loaded the file wonderfully, everything is intact and has no funky characters... My main character's weapon seems to have gone missing, though, so I need to check into that, lol. Thanks, again, everyone. If you ever need any help in the future, I'll be glad to offer whatever I can ^_^.
I'm not sure if this got properly addressed anywhere (didn't see a note about it), but since you're using newlines to delimit, you want to be using getline - it's a member function of istream:

char buffer[BIG];fin.getline(buffer,BIG);


Alternatively stop worrying about your own dynamic memory creation and use c++'s strings:

#include <string>using namespace std;string stuff;getline(fin,stuff);


Magic, no worrying about buffer sizes, allocation, etc. ((yes, you still want to be careful with your memory))

I'd also say, in response to the xml comments--screw xml, use sql ;) sqlite is a little tricky to get the hang of interfacing with, but it's beautiful. :) And public domain.
- kaolin fire- http://www.erif.org/- http://www.gudmagazine.com/

This topic is closed to new replies.

Advertisement