Ok, I am building a text based rpg. This an excerpt from my code. at one point it asks what your name is. When you type that in I put it into a string variable called name but I want to be able to save it to a binary file so it can be read later on if you shutdown the game than restart and you choose load game instead of new game.

I went through about 10 tutorials on this and none are working properly. And no i dont want to write to a txt file because people can edit the info to cheat once stats are put in there. So binary will be what I want.


int main()
{
string name;
cout << "Enter your name: ";
cin >> name;
}

so taking this string variable called name, I want to write the string to a binary file.
Any help is appreciated.

Recommended Answers

All 7 Replies

But "Fred Flintstone" reads as "Fred Flintstone" to anyone smart enough to use a hex editor.

Sure, if you write enough other binary data along with it, most simple text editors will barf on the file, but no hex editor will.

You could try encrypting the file, but by necessity, the key to the file will be in your program. So all you're really doing is reducing the set of people who would be able to hack your file format. But you're not going to reach zero people any time soon.

So, who are you trying to hide the information from, and how much effort are you willing to spend achieving it.

commented: very helpful +1

so is encryption difficult?

also when I wrote the string to a text file it would not write the whole string
when the name has a space it stops writing after the space.

Example:
Enter Name here: Jon McEwan
it would just write Jon

Is there a way around this so that it can read and write the full string

so is encryption difficult?

also when I wrote the string to a text file it would not write the whole string
when the name has a space it stops writing after the space.

Example:
Enter Name here: Jon McEwan
it would just write Jon

Is there a way around this so that it can read and write the full string

so is encryption difficult?

It depends. What level of encryption do you want? There are a lot of encryption algorithms out there, and there is C++ code to go with a lot/most of them. Here's the wikipedia link to AES encryption, which is quite powerful. A few Google queries gives you some good links to C++ code to implement it. Of course, the algorithm is only as good as your ability to hide the key, as Salem pointed out, so back to his point. Beyond actually implementing the AES algorithm in C++, if you are worried about people cheating, how hard do you want to make them work for it?

http://en.wikipedia.org/wiki/Advanced_Encryption_Standard

Here are a few more algorithms. Again, a few Google searches with "encryption algorithms" will get you some good sources.
http://www.mycrypto.net/encryption/crypto_algorithms.html

Regarding your other problem, I'm guessing that the problem is not the outputting of the data, but the inputting of the data:

int main()
{
string name;
cout << "Enter your name: ";
cin >> name;
}

The >> operator will only read till the first white space. If you type in "Fred Flintstone", "Fred" will be stored in name. So if you output name, you'll get "Fred". You're outputting the entire contents of name, but name does not include everything you entered. Use getline to read in the data. That will ignore the spaces and will read in an entire line.

http://www.cplusplus.com/reference/string/getline.html

thanks a lot for the info, it was very helpful:)

how would I use the getline function here to get the entire line of this text file and put it as a string, this is what I have right now but I cant seem to get the getline function to work with it. please help

string name2;
ifstream file("Character Data.txt");
file >> name2;
cout << name2;

how would I use the getline function here to get the entire line of this text file and put it as a string, this is what I have right now but I cant seem to get the getline function to work with it. please help

string name2;
ifstream file("Character Data.txt");
file >> name2;
cout << name2;

A stream from a file will behave just like a stream using cin. So from your code earlier and from the web link I posted, you would replace this line:

cin >> name;

with this line:

getline(cin, name);

Same thing here, except you don't have cin. You aren't getting input from the keyboard but from a file. However, the function will behave the same way. In this case you have called your ifstream file. So change the line above to:

getline(file, name2);
commented: very helpful +1

perfect, thanks a lot man.

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.