Saving binary files can have problems, as I know to my cost! They're very hard to extend, and backward and forward compatibility can be a problem, as can storing strings, since you need to allocate binary space.
There's a fairly simply way around this. I try wherever possible to use human readable formats - usually strings. If you don't want people to read them, encode them and then decode them. Here are two very simple functions to show the idea - these are in Pascal, but easily adapted to Basic or C/C++.
function Decode(s: ansiString): ansiString;
var i, v : cardinal;
begin
result := s;
if s = '' then exit;
result := '';
for i := 1 to length(s) do
begin
v := (ord(s[i]) -1);
v := v XOR I;
result := result + ansiChar(v);
end;
end;
function Encode(s: ansiString): ansiString;
var i : cardinal;
begin
result := s;
if s = '' then exit;
result := '';
for i := 1 to length(s) do
result := result + ansiChar((ord(s[i]) XOR I)+1);
end;
The Encode function takes a string and outputs another string that is XORed for each character. In this case it's using the loop index, but it can be anything.
TheDecode function does exactly the same, and returns the original string. Note: I'm using ansiString here because modern versions of Delphi use Unicode strings as the basic string type.
The text file will look like garbage to anybody looking at it:
Input:
Hello world
this is nice
We love Ancient Lady
12345
Marlène is French
Output:
Jhpik'qh|gp
vkkx&pu)hdij
Wh$ikqc)Ieifia|1^txn
11111
Mdriîic)az,K€lbtz
Passed through the Decode function this restores the original perfectly.
Notice that the numbers 12345 turn into 11111 - pretty hard to hack that! Also that I'm adding 1 to the XOR value. This is to avoid a potential null in the output string, which can be a problem with null-terminated strings.
Using this technique you don't have to allocate binary string space, and if you later introduce more values, older systems can still use what was there and ignore new stuff.
-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL