0

文字列をファイルに書き込んで、binarywriter と binaryreader を使用して読み戻しています。文字列を読み返すと、すべておかしく、非常に長く見えます。なぜそれがそうしているのかわかりません。

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;

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;

コードをデバッグすると、次のようになります。

ここに画像の説明を入力

ご覧のとおり、または表示されていない場合でも、"GroupHeader.ID" には "Groupfile" のみが含まれている必要があり、その中にゴミが含まれる長い文字列ではありません。

それで、私は何を間違っていますか?これは文字列形式のエラーですか?

4

1 に答える 1