Q&A

  • Treeview 3개의 내용을 파일로 저장하고 부르기 질문입니다.
[질문] 아래 코드에서 저장은 잘 되는데, 불러오기가 안됩니다. 자문 좀 부탁드립니다.

type
  TMyRec = Record
    Size1 : Integer;
    Size2 : Integer;
    Size3 : Integer;
end;

var
  MyRec : TMyRec;

procedure TMain.Btn_OpenClick(Sender: TObject);
var
  Stream1, Stream2, Stream3 : TMemoryStream;
  FileStream : TFileStream;
begin
  if OpenDialog1.Execute then begin
    if FileExists(OpenDialog1.FileName) then begin
      FileStream := TFileStream.Create(vFileName, fmOpenReadWrite);
    end else begin
      FileStream := TFileStream.Create(vFileName, fmCreate);
    end;

    FileStream.Read(MyRec, SizeOf(TMyRec));

    Stream1    := TMemoryStream.Create;
    Stream2    := TMemoryStream.Create;
    Stream3    := TMemoryStream.Create;

    FileStream.Read(Stream1, MyRec.Size1);
    FileStream.Read(Stream2, MyRec.Size2);
    FileStream.Read(Stream3, MyRec.Size3);

    Stream1.Seek(0, soFromBeginning);
    Stream2.Seek(0, soFromBeginning);
    Stream3.Seek(0, soFromBeginning);

    Tree1.Items.LoadFromStream(Stream1);
    Tree2.Items.LoadFromStream(Stream2);
    Tree3.Items.LoadFromStream(Stream3);

    Stream1.Free;
    Stream2.Free;
    Stream3.Free;
    FileStream.Free;
  end;
end;

procedure TMain.Btn_SaveClick(Sender: TObject);
var
  Stream1, Stream2, Stream3 : TMemoryStream;
  FileStream : TFileStream;
begin
  if SaveDialog1.Execute then begin

    if FileExists(SaveDialog1.FileName) then begin
      FileStream := TFileStream.Create(vFileName, fmOpenReadWrite);
    end else begin
      FileStream := TFileStream.Create(vFileName, fmCreate);
    end;

    Stream1    := TMemoryStream.Create;
    Stream2    := TMemoryStream.Create;
    Stream3    := TMemoryStream.Create;

    Tree1.Items.SaveToStream(Stream1);
    Tree2.Items.SaveToStream(Stream2);
    Tree3.Items.SaveToStream(Stream3);

    MyRec.Size1 := Stream1.Size;
    MyRec.Size2 := Stream2.Size;
    MyRec.Size3 := Stream3.Size;

    FileStream.Write(MyRec, SizeOf(TMyRec));
    FileStream.Write(Stream1.Memory^, Stream1.Size);
    FileStream.Write(Stream2.Memory^, Stream2.Size);
    FileStream.Write(Stream3.Memory^, Stream3.Size);

    Stream1.Free;
    Stream2.Free;
    Stream3.Free;
    FileStream.Free;
  end;
end;
0  COMMENTS