Saving data to a file in binary?

Started by
14 comments, last by andyb716 19 years, 5 months ago
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.
Advertisement
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
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.
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
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).
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

This topic is closed to new replies.

Advertisement