Skip to main content
GameDev.net gamedev.net
🔒 Locked

saving text in binary mode.

Started by GekkoCube Sep 2, 2003 at 11:55 PM 6 replies 450+ views
Original Post
GekkoCube
GekkoCube
I''m using fopen to open a file (with mode="w") to simply write text to a file. I would like to saving this file in binary so that i save space and so nobody can just open it and read it. i tried mode="wb", but i can still read it, although not formatted correctly. is there a way to save it in a binary format uncipherable by the human eye at first glance?
Nypyren
Nypyren
The absolute easiest way to hide the data from a first-glance overview from someone like me is to XOR the data with a decently long key... (so that 00 00 00 00 doesn't show up as 7D 7D 7D 7D in the file -- people like me can see that right away as being zeroes)

It's not that hard to BREAK, but to just look over the file casually you won't see anything important.

[edited by - Nypyren on September 3, 2003 1:01:58 AM]
GekkoCube
GekkoCube
well, its not like im trying to encrypt anything.
im just trying to save in plain old binary format.

this brings up the question: what is the benefit of saving a text file in binary?
Nypyren
Nypyren
Strings will look the same in binary and text writing modes, except that binary mode you should either prefix a string with its length or append a byte 0x00 to the end of the string (like strings have in memory). In a text file, you have a newline or some significant text character that signifies where the end of a string is.

Integers and floats will look totally different (and fit smaller -- 4 bytes can fit a 10-digit int.)

I'm guessing that you use the functions fprintf/fscanf or gets/puts or something like that currently. In binary mode, I usually use fwrite/fread, and ftell and fseek for moving the file pointer around in the file. (the file pointer designates where the next read or write will take place)


The benefits to binary mode is that you're making it so that a user can't just open up your file in notepad and screw it up (put in a newline that you weren't expecting or something)

The downside is that you can't just open up notepad to create a necessary file -- you have to have a hexeditor or one of your programs generate the file.

[edited by - Nypyren on September 3, 2003 1:10:18 AM]
GekkoCube
GekkoCube
yeah, i used fprintf to write to the file, which made everything into a string.

thing is i am writing some text and mostly numbers.
how would you recommend me write them all out?

currently, like i mentioned, im converting the numbers to a string by using fprintf.
TravisWells
TravisWells
Your question cannot be answered as given because it is based on assumptions that are not true

The difference between text & binary mode, in fopen (and iostreams, I think):

Text: On write, \n turns into \r\n. On read, \r\n turns into \n.
Binary: \n is still \n.

(That''s for windows/dos, it''s a bit different on mac/*nix)

So...
FILE *fp=fopen("out.txt","w");
or
FILE *fp=fopen("out.txt","wb");
then:
fputs("test");
fclose(fp);

The files will be exactly the same.
So if you want to prevent some text from being read, you need to do some kind of encryption on it (Even something very simple)
GekkoCube
GekkoCube
so you''re telling me that reading form a binary file would be the same as reading from a text file (assuming the same thing was written except in different modes)?
Nypyren
Nypyren
Say you have the following data:


char *str;
int i1, i2, i3;
float f1, f2, f3;


and you want to store it in that order in the file, here's how I would do it (length-of-string-prefix for strings):


void SaveIt()
{
FILE *f = fopen("blah.dat", "wb");
short len = strlen(str); // let's assume that it won't be too long.
fwrite(&len, 2, 1, f);
// the two middle numbers are multiplied to determine the amount of bytes to write.
// shorts are 2 bytes, so I say 2,1. Technically you can use an int and write only 2 bytes...

fwrite(str, strlen(str), 1, f);
fwrite(&i1, 4, 1, f); // some people like to use "sizeof(int)"
fwrite(&i2, 4, 1, f);
fwrite(&i3, 4, 1, f);
fwrite(&f1, 4, 1, f); // or sizeof(float)
fwrite(&f2, 4, 1, f);
fwrite(&f3, 4, 1, f);

fclose(f);
}


void LoadIt()
{
FILE *f = fopen("blah.dat", "rb");

short len;
fread(&len, 2, 1, f);

// I'll use C++ new here, replace with malloc if you're in C
str = new char[len+1];
fread(str, len, 1, f);
str[len]=0; // manually null-terminate

fread(&i1, 4, 1, f);
fread(&i2, 4, 1, f);
fread(&i3, 4, 1, f);
fread(&f1, 4, 1, f);
fread(&f2, 4, 1, f);
fread(&f3, 4, 1, f);

fclose(f);
}


-edit- Sorry for hosing the thread width...

-prev post edit-

Yes, if you write just a string in binary or text modes, it will be almost exactly the same (depending on the newline funkiness). You don't *have* to open a file with "b" to fwrite/fread, but if you use fscanf or fgets in binary read mode, the newline differences can screw you up.

The main reason I use binary files is to store numbers much more neatly.

[edited by - Nypyren on September 3, 2003 1:26:07 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.