Sign in to follow this  
andyb716

Saving data to a file in binary?

Recommended Posts

Hi, I'm using c++ and the only way I could write and read in binary is the std::fstream. I've tried the FILE *file. and the std::ifstream and ostream and I couldn't get them to write in binary. What I need it for is to save scores for my game, If I use text then a player can go edit there scores and say they got a billion points on level one. What I have now is that is saves a couple scores correctly and then once I get the third score in there some of it gets erased or doesn't pull up properly. When I view the file its all on one line. Is that ok, because I try to put \n's to break the different scores. I seperate the name level and score with a blank space and then get the whole line with getline and the break apart the line with substrings. Maybe it putting the whole line in there and not finding line breaks. Can someone please tell me what file methods I should use and what ones can write binary? I've read on google they all can but I just can't get it to work, besides using fstream Thanks.

Share this post


Link to post
Share on other sites
well with C you would use fread() and wb or rb to make it binary.
and for C++ i think you need to use the qualifier ios::binary.

this is what i remember from a few years ago.

anyway if you [google] the underlined you should find more info

Share this post


Link to post
Share on other sites
std::ofstream fout("highscores.bin", std::ios::out | std::ios::binary);

int highScores[10], numHighScores = 10;

fout.write((char *)&numHighScores, sizeof(int));

fout.write((char *)highScores, numHighScores * sizeof(int));
...
std::ifstream fin("highscores.bin", std::ios::in | std::ios::binary);

fin.read((char *)&numHighScores, sizeof(int));

fin.read((char *)highScores, numHighScores * sizeof(int));

Share this post


Link to post
Share on other sites
Why would it show up as text? You can edit it in a text editer, but shouldn't it show up as misc. ASCII characters isntead of a string of human-readable numbers?

Share this post


Link to post
Share on other sites
Write a test example that doesn't work and show us the code. The compilable code shouldn't need to be more than about 10 lines.

Pete

Share this post


Link to post
Share on other sites

#include <fstream>
#include <iostream>

std::ofstream outFile;

int main(){

char *String = "This is some text\n This is some more text.";

outFile.open("textfile.bin", std::ios::out | std::ios::binary);

outFile.write(String, strlen(String));

outFile.close();

return 0;
}

Share this post


Link to post
Share on other sites
Writing a string to a binary file still lets you read that string. Open up a .zip file in notepad and you will see the first 2 characters are PK, which are part of the zip header, and you will likely be able to pick out readable strings of file names and/or folders.

Share this post


Link to post
Share on other sites
Of course that shows up readable in a file. I think you're misunderstanding the difference between plaintext and binary. When you write a character (for example 'a') to a file in either mode the binary value 01100001 (97) is written (assuming ASCII). Reading this in plaintext will correctly interpret that binary value as an 'a'. However, writing the value '100' in plaintext will write the three binary values 00110001 (49), 00110000 (48) and 00110000 (48) (ASCII for '1', '0' and '0'), whereas writing it in binary as a four-byte integer will write the binary values 00000000 (0), 00000000 (0), 00000000 (0) and 01100100 (100), which would be interpreted in plaintext as three null characters followed by the letter 'd'.

Enigma

Share this post


Link to post
Share on other sites
The only real difference between opening a file in text mode or binary mode is the way newlines get interpreted: in text mode, outputting a '\n' is changed into outputting a "\r\n" and vice-versa for inputting. In either mode, strings are text and will appear the same. Numbers and data structures, on the other hand, are stored very differently, depending on whether you output them using operator << (they will appear as text) or writing the raw binary data (using the write function). For example, the number 1 million would be written as "1000000" in text mode, taking up 7 bytes (not counting a space to separate it from other data), whereas in binary, it will always take up exactly 4 bytes, in this case it would be stored as 0x40 0x42 0x0F 0x00 (Windows is little-endian). If you want to write text to a binary file and have it not be human-readable, you're going to have to add some sort of encryption. The level of encryption you use depends on how hard you want it to be to hand-edit the data.

Share this post


Link to post
Share on other sites
Here's a radical idea:

Use SQLite, and let it handle the file manipulation for you.

Download SQLite, build it to a static library, link you're code against it and BOOM, instant in process DB backend. Storing a score becomes:

"insert into scores values( 'steve_o', '199039' );"

Getting values out becomes:

"select * from scores;"

td

Share this post


Link to post
Share on other sites
As the previous posters said, when you put ASCII characters into a binary file, they're still ASCII characters, and still readable in any text editor. Binary mode doesn't somehow scramble the bits that make up ASCII characters.

Try writing out some int and double variables and compare text mode to binary mode. In text mode, you would see those variables in clear text (since it's converting the actual numbers to text), and in binary you would see some garbage (since it's outputting the bits directly).

Share this post


Link to post
Share on other sites
I was looking at some old code of mine, that is a Text RPG that loads/saves a players charcter. It is a class that holds different info. I just use FILE *fp fread(&Player, etc); and fwrite(&Player, etc); It saves the whole player object and so I dont need to read in different variables. However in this case i need to have a class that has 10 different names, levels they got to and scores they got. My thinking was making a struct to hold the data and then in the class do something like this sHighScores HighScore[10]; I've tried doing that but It seems to not save. I can probably make a simple test project to do it, but If anybody can tell me if I'm doing this all wrong please let me know, I really dont know squat when it comes to this kind of programming. thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this