I'm no doubt missing something basic here, but I can't figure out how to write (and later, read) data from an object to a binary file. Maybe I should be doing something completely different? Anyhow, here's what I'm doing:
var
ConfigDir : String;
fsOut : TFileStream;
ConfigSize : Integer;
begin
...
[note: ProgConfig is an object that contains integers, strings, and some enumerations]
ConfigSize := ProgConfig.InstanceSize;
fsOut := TFileStream.Create( FileName, fmCreate );
fsOut.Write( ConfigSize, sizeof( Integer ) );
fsOut.Write( ProgConfig, ConfigSize );
fsOut.Free;
...
end;
The file it writes does not appear to contain the data it should. I look at it with a hex editor, and I don't see any of the strings there.
Here's the code I'm using to read it back in:
var
ConfigDir : String;
fsIn : TFileStream;
ConfigSize : Integer;
begin
Result := True;
ConfigDir := GetAppConfigDir( False );
...
begin
fsIn := TFileStream.Create( FileName, fmOpenRead );
fsIn.Read( ConfigSize, sizeof( Integer ) );
fsIn.Read( ProgConfig, ConfigSize );
fsIn.Free;
end;
...
Clearly I'm doing something very wrong here. Am I completely on the wrong track? If so, what should I be doing? Must I convert everything to a string and save it as a text file? Thanks in advance for any help!