I am writing a string into a file and reading it back out using binarywriter and binaryreader. When the string is read back out, it looks all funny and very long. I don't know why it is doing that.
Here is how I write into a file with binarywriter:
TFileHeader = Record
ID:String;
version:SmallInt;
end;
method WriteGroups(fname:string; cg:ArrayList);
var
strm : BinaryWriter;
i:integer;
cnt:Integer;
GroupHeader:TFileHeader;
begin
GroupHeader.ID:='GroupFile'; <<<-----I am having problem with this string.
GroupHeader.version:=6;
if Environment.OSVersion.Platform = System.PlatformID.Unix then
fname := baseDir +'/calgroup.dat'
else
fname := baseDir +'\calgroup.dat';
if cg.Count > 0 then
begin
strm := new BinaryWriter(File.Create(fname));
cnt := cg.Count;
strm.Write(GroupHeader.version);
strm.Write(GroupHeader.ID);
strm.Write(cnt);
for i := 0 to cnt - 1 do
begin
TCalGroup(cg[i]).WriteMGroup(strm);
end;
strm.Close;
end;
end;
Here is how I read from a file using BinaryReader:
method ReadGroups(fname:string; cg:ArrayList);
var
strm : BinaryReader;
GroupHeader:TFileHeader;
cnt:SmallInt;
i:integer;
cgp:TMagiKalCalGroup;
begin
GroupHeader.ID :='';
GroupHeader.version := 0;
if Environment.OSVersion.Platform = System.PlatformID.Unix then
fname := baseDir +'/calgroup.dat'
else
fname := baseDir +'\calgroup.dat';
if File.Exists(fname) then
begin
ClearGroups(cg);
strm := new BinaryReader(file.OpenRead(fname));
GroupHeader.version:=strm.ReadInt32;
GroupHeader.ID := strm.ReadString; <-----Here is the problem. See the image below..
if ((GroupHeader.ID='') or (GroupHeader.version>100)) then
begin
strm.Close;
Exit;
end;
if (GroupHeader.version<5) then
begin
strm.Close;
exit;
end;
cnt := strm.ReadInt16;
for i := 0 to cnt - 1 do
begin
reformat:=false;
cgp := new TMagiKalCalGroup('New Grp');
cgp.ReadMGroup(Strm);
cgp.UpdateDateTime(System.DateTime.Now);
cg.Add(cgp);
end;
//strm.Free;
strm.Close;
end;
end;
Here what I see when I debug the code:
As you can see or not see, "GroupHeader.ID" should only contain "Groupfile" not long string with garbage in it.
So, what am I doing wrong? Is this a string format error?
Smallint
is a 16-bit value. When reading the file, you are reading that value as a 32-bit value instead of a 16-bit value, so you end up reading some of the bytes that belong to the length of the stored string. When you then read the string, the first 2 bytes of the string characters get interpreted as part of the string length, which is why you end up with garbage.
You have a similar logic bug when reading the groups. Integer
is a 32-bit value. When reading the group count, you are reading it as a 16-bit value instead, which means your group reads are going to be off by 2 bytes and will be corrupted as well.
You need to change these lines inside of your ReadGroups()
function:
cnt: Smallint;
...
GroupHeader.version:=strm.ReadInt32;
...
cnt := strm.ReadInt16;
To these instead:
cnt: Integer;
...
GroupHeader.version := strm.ReadInt16;
...
cnt := strm.ReadInt32;