Advertisement

Save / Load in text game

Started by February 11, 2003 09:40 AM
8 comments, last by Betrayer_of_Code 21 years, 9 months ago
I am making a text game as some might know, but I have a problem I have no way to save / load a game, unless I trust the player. When you kill the big bad at the end of quest one, it gives you a pass and writes all of your stats to a file. Then, when the player wants to load a game he/she restarts the game and they get the option to re-load the game. I triedusing ofstream.h to read the text, but it will only go to strings, not to ints and chars. Is there a way I can do this, with out trusting poeopl to not ruin the game???? Thanks Homepage Luck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride. For any total noob out there Here is a like to my tutorials, thry them out
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
you can read and write with binary, but anyone who actually cares to beat your game could get past that easily...look into encryption if you are that serious but i dont think its worth it. Try binary.

Brian
Brian J
Advertisement
Yeah, I agree. If you''re just making a text game, I wouldn''t even bother. It might be fun to try encryption anyway, but I doubt you''ll really need it. Even commericial games can''t stop "cheating" in single player, so...

Peon
Peon
I''m currently working on an adnvaced version of hangman, and I''m encrypting my highscore list just for fun''s sake...

I was thinking about reading in a string of x field length and then read single fields out of it to create the whole.
Means you take an a from spot x, a 0 from spot y and a 1 from spot z etc...
If anyone cared enough he could hack the thing, but who would bother for hangman? Take a look at that point of view, not the other way around
I didnt mean how to make it un hackable, or even encrypted. But how to actually make a load function. Right now once they type in the password, they are suposed to take info from a certain file, and type it into the neccasary file, I want to skip that step and have the program read the file and asign a certain line to player.gold, and a certain to player.str etc...

Homepage
Luck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
For any total noob out there Here is a like to my tutorials, thry them out
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
Are you asking how do you read from a file? If you are, you can use the ifstream class, found in the fstream header:


        #include <iostream>#include <fstream>#include <string>// ...std::ifstream inputFile("file_with_data_in.txt");if (inputFile.fail()){    // things to do upon failure    // for example:    std::cerr << "Error: Could not open file_with_data_in.txt!\n\n";    system("pause");    return -1;}// Then you can use your ifstream object// just as you would std::cinstd::string word;while (inputFile >> word)     // get input word by word, skipping spaces, until end of file{    // do something with words}std::string line;while (std::getline(inputFile, line, '\n'))    // place all input in line until '\n' character is reached{    // do something with the line}// ...        


[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on February 11, 2003 3:29:53 PM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
Advertisement
how about holding the data in a struct and then saving the whole struct to a file?

TG Ramsus
TG Ramsus
NO!!!!
I know how to read form file...
Once again
My program writes the info to a file, then i want it to read form the file, but instead of utting a certain part into a string I want it to put that part into int, then another part to string, then another to char...But It cant, So I need to know a new way to read from file, that will put the frist line to int, the second to string and third to char
!!!!!!!!!!!!!!!!1
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
Read it all in as strings and then:
convert the first line to int, second leave as it is and third to char.
If that is not what you want then You''re not making yourself very clear.

So, what''s all that business about trusting the user than?

  #include <string>#include <fstream>#include <sstream>int main(){  std::ifstream ifs( "myfile.txt" );  std::istringstream iss;  std::string line;  int first;  std::string second;  char third;  std::getline( ifs, line );  iss.str( line );  iss.clear();  iss >> first;  std::getline( ifs, line );  iss.str( line );  iss.clear();  iss >> second;  std::getline( ifs, line );  iss.str( line );  iss.clear();  iss >> third;}  


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement