Is there a way to save in a text based game through the MSDOS window? I want to be able to save the variables that store the mana, health, ect of a player.

Dani AI

Generated

A few practical points to make saves reliable and maintainable beyond the basic examples already posted by and .

Start with a simple, versioned format. Add a short header (magic tag + format version) so loads can detect incompatible files and run a migration or fail cleanly. Save canonical state only (HP, XP, inventory contents, RNG seed); recompute derived values (max HP from level, temporary buffs) when loading. Human-readable key/value lines are easiest to debug; a small binary format can be used later if space or speed matters.

Careful with raw struct dumps. ’s idea of writing a whole struct at once is fast but brittle: compiler padding, alignment, pointer members, and endianness will break saves between builds or platforms. If a binary approach is needed, explicitly serialize primitives and container sizes, include the header/version, and add a checksum or simple CRC so corrupted files are detected.

Safe-write pattern (avoid truncate-on-crash): write to a temporary file, flush and close it, then atomically rename it over the old save. Example pattern:

#include <fstream>
#include <filesystem>
#include <string>
#include <system_error>

bool atomic_save(const std::string &path, const std::string &payload) {
    std::string tmp = path + ".tmp";
    {
        std::ofstream ofs(tmp, std::ios::binary);
        if (!ofs) return false;
        ofs.write(payload.data(), payload.size());
        if (!ofs) return false;
    } // ofs closed and flushed here
    std::error_code ec;
    std::filesystem::rename(tmp, path, ec);
    if (ec) std::filesystem::remove(tmp, ec);
    return !ec;
}

Operational tips: keep multiple save slots or automatic backups, validate ranges on load and handle failures gracefully, and modularize serialization separate from I/O so the same serializer can be used for autosave, manual save, or network sync. As noted, file I/O is fundamental — adding versioning, validation, and atomic replacement makes the system robust as the game evolves.

Recommended Answers

All 11 Replies

Sure.
All you need to decide is which variables you have in your game which are necessary to restore the state of the game.

You would use ifstream to read data (much like cin), and ofstream to write it (like cout). (note: cout and cin are objects of classes that are already made. ifstream and ofstream are actual classes).

To write data:

ofstream fout("file.sav");
fout << health << "\n" << mana << "\n" << etc;

to read data:

ifstream fin("file.sav");
fin >> health >> mana >> etc;

or make one class game_state and then output.write(&game_state, sizeof(game_state)) and to restore input.read(&game_state, sizeof(game_state))

If their stats are increasing as they progress through the game, how do you write it to the save file when they tell it to?

Er, there seem to be some awfully large gaps in your basic C++ knowledge to be writing a game.

File I/O is the first thing you typically learn how to do.

Nvm...

Er, there seem to be some awfully large gaps in your basic C++ knowledge to be writing a game.

File I/O is the first thing you typically learn how to do.

It wasn't for me.

Alright thanks, but what directive do you use to use ofstream and ifstream? If their stats are increasing as they progress through the game, how do you write it to the save file when they tell it to?

The code I showed you

1.
      ofstream fout("file.sav");
   2.
      fout << health << "\n" << mana << "\n" << etc;

is all you need to write the variables health, mana, and etc to a file. If you want to add more/different variables (im assuming you do), just add them following the above pattern. Make sure to #include <fstream> at the top of your file.

ok thanks

If you want to be able to save whenever you want to, all you need to do is make a save 'function'. Modularizing your code will make it much easier to work with, change, and improve. Say you have a struct 'hero' with variables strength, health, and exp. Your program might look something like this:

#include <iostream>
#include <fstream> //necessary for file i/o
using namespace std; 

struct hero {
  int str, hp, exp;
}

void saveGame(struct hero Zotan);

int main() {
  hero Zotan;
  Zotan.str = 10; Zotan.hp = 100; Zotan.exp = 0;
  char input;

  do { //this is a basic menu

    cout << "Choose an Action:\n
                  S - Save\nQ - Quit Game\n\n";
    cin >> input;
    switch(input) {
      case 'S':
        saveGame(Zotan);
        break;
      case 'Q':
        break;
    }
  }while(input != 'Q');

return 0;
}

void saveGame(struct hero Zotan) {
  ofstream saveData;
  saveData.open("gamedata.txt", ios::out);
  saveData << Zotan.str << "\n" << Zotan.hp << "\n" << Zotan.exp;
  saveData.close;
}

And there it is, basic file saving whenever you want. If you haven't yet read up on file i/o. It is very important stuff to know. Remember, modularizing is the key to a good program.
P.S. I'm not sure if that code will compile, so don't just copy it onto your developer. I wrote for you to use as a reference.
P.S.S. Im only 16 so don't criticize me too bad; I've been programming less than a year but I've got the basics down. Thanks everyone!

I did get the whole saving thing figured out but your way is definitely a different way than I did.

Thanks

I'm not exactly sure, but I think now you mark this thread as "solved", and then it says I'm more helpful at solving problems. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.