[C++] Saving

Junior Spellweaver
Joined
Jun 10, 2008
Messages
117
Reaction score
0
I'm making a Text RPG and i need to know how i can save data onto a file and recall it back so i can have the person start from where they left off.

for example:


I reach level 5

i need to save game

(save to what?)
(how)

--------------------
the next day

i want to load my character

(load file)
(how)
 
yea the only thing about that website is it doesn't tell you if it works with int's long's et cetera. cause thats the data i need to save and recall. I am also trying to get it to be encrypted.
 
First you will have to think about a save file format, then you can write and read it from a file.

Make a Class for that, with method to read or write the save.

Make it work first and then think about encrypting it.
 
Assuming your file is already appropriately opened, one way to write/read a single int:

int number = 5;
char* address = static_cast<char*> &number; // get the address for variable number, should be cast
// to avoid a warning for "wrong" type
file.write(address, sizeof(number)); // write enough bytes so all of number is written

...
int number = 0; // just an initialization, 0 isn't important
char* address = static_cast<char*> &number; // get the address for variable number
file.read(address, sizeof(number); // read to the address of number, but you must know the currect place to be read at is where number was stored

Some(?) compilers might(?) accept the calls directly as file.write(&number, sizeof(number)).
You can use the same code also if your data to be saved is a struct with several values instead of simple int.
 
You all think i don't know how to open a file.... I have read the site. the reason i was asking about it was bacause on the site
http://www.cplusplus.com/doc/tutorial/files/
it doesn't say anything about saving and recalling integers.

We did not say you do not know, you got a bad attitude, but I'm here to help so I will.
Code:
#include <sstream>

Use this function to convert your int or whatever to char* so you can write it to the file.
Code:
template<class T>char* toChar(T element)
{
    std::ostringstream oss;
    oss << element;

    char *c = new char[oss.str().length() + 1];
    std::strcpy(c, oss.str().c_str());

    return c;
}

Now show some respect to those helping you.
 
Sorry i was talking about Negeta

Assuming your file is already appropriately opened, one way to write/read a single int:

I already figured out how to do everything the only problem is letting people mess with the file. I want to find a way to encrypt the file and still let the program read it.
 
Often the whole file, or a large part of it, is decrypted at once so you can use any part of it directly in memory. Once you quit the program, only then encrypt the data again and save to file.

As others already mentioned, you do have quite an ungrateful attitude. Not sure if we've ever seen single thanks even though everyone's been babysitting you for several weeks. The fact is we all do this for fun, but treating everyone like they owe you won't get you far.
 
Sorry Negeta
I have not been in the greatest mood lately. I've been having problems at home and I'm sorry that it is reflecting what I type. I will try to be a little more appreciative.
 
Back