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?