# Saving Option

## Recommended Posts

Hello, I am making a DOS game and I was wondering.. how would I make it if a user presses the S key then it will save their progress? IDE: Dev-C++ Language: C++

##### Share on other sites
You would use File I/O. When the user presses s, you would save the important variable information to a file.

If you need an example, then just ask.

##### Share on other sites
If this is a console program, than I would wager to say that the only time the program pauses is when it waits for user input. In this case, the easiest (but perhaps not the most elegant) solution then would be to provide an if statement to check if the user wants to save instead of continuing.

Basically, every time you read input, you have to remember to perform this check. Which is why it's somewhat inelegant; but the only other solution I can think of is to use multithreading, and have one thread be devoted entirely to catching the 's' key. But that opens up a whole other can of worms: how would you know that the 's' keypress represents a request to save, and not simply input to a prompt? How would you stop the main thread from continuing if there is a request to save? etc. etc.

So, yeah, just stick to the first option [smile]

##### Share on other sites
Quote:
 Original post by Chad SmithYou would use File I/O. When the user presses s, you would save the important variable information to a file.If you need an example, then just ask.Chad

Can I get an example please? :P

##### Share on other sites
To use file i/o in C++, you will need to use ifstream (for reading) and ofstream(for writing), a very simple example:

#include <fstream> // for ifstream and ofstream#include <iostream>// creates an ofstream object associated with somefile.txt and open the file:std::ofstream output("somefile.txt");if (!output){    std::cout << "Oh no, an error occured!";    //do something about it}output << "this text will now be in somefile.txt";output.close(); // file will be closed when output gets destroyed on scope exit, this will close it right now.

ifstream is similar.

Another option is to use xml, which I find personally easy to use for these purposes. This way you don't have to deal with a lot of issues when reading a file. There is a fine and small library for this, TinyXml, and the xml part of this gamedev article might help you.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627689
• Total Posts
2978659

• 18
• 14
• 12
• 10
• 12