This topic is now archived and is closed to further replies.


C++ Loading .txt

Recommended Posts

TwisteR    122
Hi, I am designing a text based game that will require the option to save. I have desided that it should save in a .txt format (do to every computer has a .txt viewer [i think!!]). I was wondering how I would save data to a .txt file so the users game progress will not be lost. I am writting the code in Visual C++ 6.0 if that helps any.. Oh and if you could help me load it back up to that would be VERY helpful.......... Thank you. ----------------------------- -------Mitch Mulholland------ ----A.K.A.= TwisteR---------- -----------------------------

Share this post

Link to post
Share on other sites
jinushaun    122
1 - Create a file format (make it as simple or as complicated as you want)
2 - Figure out how you would write it out, and read it back (scanning/parsing)
3 - implement it

As for the C++ for this:

#include <fstream>
using namespace std;

int main()
ifstream inFile("fileToRead.txt");
ofstream outFile("fileToWriteTo.txt");

string data;
inFile >> data; // read something in, same ops as cin

outFile << "name = value\n"; // write it out in name/value pairs

return 0;

The format you choose to write it out in decides how complicated reading back in will be.

Say you have this format:

name = value
name = value

The spaces in between help to make reading it back easier. ifstream >> reads in each string separated by a space. Having each name/value pair on its own line also makes it easy to read back in.

If you want to make it complicated, give it structure:

someGlobalVar = "";

player {
name = "John Doe";
hp = 144;
exp = 10;
// ...


Share this post

Link to post
Share on other sites