Hi. I am writing a text-rpg game using c++ and I want to save the gave somehow. How can i do it?

Thank you.

Write all the important vars to a file. You could use ofstream for that.
example

can you find an example code for me?
Thx.

How about the EXAMPLE link I posted in my previous post? It's the blue (underlined when hovering over it) thingy that says 'example'. Click it with your mouse.

i kind of doesn't get that.. sorry

i kind of doesn't get that.. sorry

I sincerely hope that you're talking about the code. Because if you don't get how links work....

But seriously: If you don't get that example, you're not ready to make a game.
But I'll try to explain the code. Here's the code with my comment in it:

#include <iostream> 
#include <fstream> 
using namespace std; 
 
int main() 
{ 
  ofstream out("test"); // create a file 'test' 
  if(!out)  // if it didn't succeed in creating (and opening) the file
  { 
    cout << "Cannot open file.\n";  //print 'cannot open file'
    return 1;  // and exit the program
   } 
 
// write 10 and 123.23 to the file separated by space
out << 10 << " " << 123.23 << endl; 
// write "This is a short text file." to file
out << "This is a short text file.";   
 
out.close(); //close the output file
 
return 0; //exit
}

now open the file and see what's in it.
ofcourse you can change 'test' to c:\\test.txt or something that you like

How to save the game depends on how the program is written. Since thre are over a gzillion ways to write a game its impossible for anyone here to tell you exactly how you do it. But lets say your program has two variables that need to be saved and later read back

class MyGame
{
private:
    int a;
    std::string b;
};

There are several different ways you could write it, but the quickest way is to write the whole class as binary data.

MyGame  game; // the game object
//

ofstream out("mygame.bin", ios::binary);
out.write((char *)&game, sizeof(MyGame));

can you also write in text or only binary?

If you would look at my example, you see that I write plain text in it. (line 17)

can you also write in text or only binary?

If you will write the data as Ancient Dragon showed, then use the binary mode only.

Below is a small example of writing and reading in binary mode.
Note that the MyGame class does not contain a std::string but instead a fixed size char array for storing a string (str1).

#include <iostream>
#include <fstream>
#include <cassert>
using namespace std;

class MyGame
{
public:
	MyGame(int n = 0, const char * p1 = "")
	{
		a = n;
		strcpy(str1, p1);
	}

	bool operator == (const MyGame & rhs)
	{
		// compare the objects and tell if they are equal
		return (this->a == rhs.a) && (0 == strcmp(this->str1, rhs.str1));
	}

private:
    int a;
    char str1[20];
};

enum { SIZE_OF_MYGAME = sizeof(MyGame) };

int main(int c, char ** p)
{
	// two objects
	MyGame game1(1, "first");
	MyGame game2(2, "second");

	ofstream out("mygame.bin", ios::binary);

	// write the two objects
	out.write((const char*)&game1, SIZE_OF_MYGAME);
	out.write((const char*)&game2, SIZE_OF_MYGAME);
	out.close();

	ifstream in("mygame.bin", ios::binary);

	// two objects to read in
	MyGame game3;
	MyGame game4;
	
	// read the two objects
	in.read((char*)&game3, SIZE_OF_MYGAME);
	in.read((char*)&game4, SIZE_OF_MYGAME);

	assert(game1 == game3); // check that the objects are equal
	assert(game2 == game4); // check that the objects are equal

	return 0;
}
#include<iostream>
#include<fstream>
using namespace std;

class Game
{
private:
    int a;
    
public:



};class Game


Game  game; // the game object
//int main()
{


ofstream out("game", ios::out | ios::binary | ios::app);
out.write((char *)&game, sizeof(Game));
out.close();



}

Below is a small example of writing and reading in binary mode.
Note that the MyGame class does not contain a std::string but instead a fixed size char array for storing a string (str1).

The example I posted was wrong about that. binary write doesn't work with c++ objects like std::string

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.