Solved

Read/Save contents

Posted on 2001-09-14
2
223 Views
Last Modified: 2010-04-06
How can I use TStream to save and read the contents of 4 listboxs and 2 memos to "Demo.dat" file? Something like database. Exmaples please.
0
Comment
Question by:TCSCode
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Expert Comment

by:bugroger
ID: 6483809
Hi,

Have a look at these two functions:

{
 SaveIT(ListBox1, ListBox2, ListBox3, ListBox4, Memo1, Memo2, 'C:\Demo.dat');
LoadIt(ListBox1, ListBox2, ListBox3, ListBox4, Memo1, Memo2, 'C:\Demo.dat');
}

Procedure SaveIT(LB1, LB2, LB3, LB4 : TListBox; M1, M2 : TMemo; FileName : String);
VAR
 FS           : TFileStream;
 DummyStream  : TMemoryStream;
 Size         : Integer;

Begin
 FS          := TFileStream.Create(FileName, fmCreate or fmSHAREDENYNONE);
 DummyStream := TMemoryStream.Create;

 LB1.Items.SaveToStream(DummyStream);
 Size := DummyStream.Size;
 FS.Write(Size, 4);
 FS.Write(DummyStream.Memory^, Size);

 LB2.Items.SaveToStream(DummyStream);
 Size := DummyStream.Size;
 FS.Write(Size, 4);
 FS.Write(DummyStream.Memory^, Size);

 LB3.Items.SaveToStream(DummyStream);
 Size := DummyStream.Size;
 FS.Write(Size, 4);
 FS.Write(DummyStream.Memory^, Size);

 LB4.Items.SaveToStream(DummyStream);
 Size := DummyStream.Size;
 FS.Write(Size, 4);
 FS.Write(DummyStream.Memory^, Size);

 M1.Lines.SaveToStream(DummyStream);
 Size := DummyStream.Size;
 FS.Write(Size, 4);
 FS.Write(DummyStream.Memory^, Size);

 M2.Lines.SaveToStream(DummyStream);
 Size := DummyStream.Size;
 FS.Write(Size, 4);
 FS.Write(DummyStream.Memory^, Size);

 DummyStream.Free;
 FS.Free;
End;

Procedure LoadIT(LB1, LB2, LB3, LB4 : TListBox; M1, M2 : TMemo; FileName : String);
VAR
 FS           : TFileStream;
 DummyStream  : TMemoryStream;
 Size         : Integer;

Begin
 FS          := TFileStream.Create(FileName, fmOpenRead or fmSHAREDENYNONE);
 DummyStream := TMemoryStream.Create;

 FS.Read(Size, 4);
 DummyStream.Size := Size;
 FS.Read(DummyStream.Memory^, Size);
 LB1.Items.LoadFromStream(DummyStream);

 FS.Read(Size, 4);
 DummyStream.Size := Size;
 FS.Read(DummyStream.Memory^, Size);
 LB2.Items.LoadFromStream(DummyStream);

 FS.Read(Size, 4);
 DummyStream.Size := Size;
 FS.Read(DummyStream.Memory^, Size);
 LB3.Items.LoadFromStream(DummyStream);

 FS.Read(Size, 4);
 DummyStream.Size := Size;
 FS.Read(DummyStream.Memory^, Size);
 LB4.Items.LoadFromStream(DummyStream);

 FS.Read(Size, 4);
 DummyStream.Size := Size;
 FS.Read(DummyStream.Memory^, Size);
 M1.Lines.LoadFromStream(DummyStream);

 FS.Read(Size, 4);
 DummyStream.Size := Size;
 FS.Read(DummyStream.Memory^, Size);
 M2.Lines.LoadFromStream(DummyStream);

 DummyStream.Free;
 FS.Free;
End;
0
 

Accepted Solution

by:
duti earned 300 total points
ID: 6484085
const
  sFileName ='demo.dat';
{---------------------------------------------------------------------------
  TForm1.Button1Click
---------------------------------------------------------------------------}
procedure TForm1.Button1Click(Sender: TObject);
Begin
  // you can save as much listboxes/memos as you want,
  // without additional coding in the save/load routines.
  SaveData( [ ListBox1, Memo1, ListBox2, listbox3, Memo2 ] );
end;

{---------------------------------------------------------------------------
  TForm1.Button2Click
---------------------------------------------------------------------------}
procedure TForm1.Button2Click(Sender: TObject);
begin
  LoadData; // everything works automagically
end;

{---------------------------------------------------------------------------
  TForm1.SaveData
---------------------------------------------------------------------------}
procedure TForm1.SaveData(aComp : array of TComponent);
var
  stream : TFileStream;
  i      : integer;

  procedure _Save(const sName, sData : string);
  var iLength : integer;
  begin
    iLength := length(sName);
    stream.Write(iLength, sizeof(iLength));
    stream.Write(pchar(sName)^, length(sName));

    iLength := length(sData);
    stream.Write(iLength, sizeof(iLength));
    stream.Write(pchar(sData)^, length(sData));
  end;
begin
  stream := TFileStream.Create(sFileName, fmCreate or fmShareDenyNone);
  try
    for i := low(aComp) to high(aComp) do
      if aComp[i] is TListbox then _Save(aComp[i].Name, TListbox(aComp[i]).Items.Text)
      else if aComp[i] is TMemo then _Save(aComp[i].Name, TMemo(aComp[i]).Lines.Text);
  finally
    stream.Free;
  end;
end;

{---------------------------------------------------------------------------
  TForm1.LoadData
---------------------------------------------------------------------------}
procedure TForm1.LoadData;
var
  stream       : TFileStream;
  iLength      : integer;
  iBytesRead   : integer;
  sName        : string;
  sText        : string;
  Comp         : TComponent;
begin
  stream := TFileStream.Create(sFileName, fmOpenRead or fmShareDenyNone);
  try
    repeat
      iBytesRead := stream.Read(iLength, SizeOf(iLength));
      if iBytesRead > 0 then
        begin
          SetLength(sName, iLength);
          stream.Read(pchar(sName)^, iLength);

          stream.Read(iLength, SizeOf(iLength));
          SetLength(sText, iLength);
          stream.Read(pchar(sText)^, iLength);
          Comp := FindComponent(sName);
          if Comp <> nil then
            begin
              if Comp is TListBox then TListBox(Comp).Items.Text := sText
              else if Comp is TMemo then TMemo(Comp).Lines.Text := sText;
            end;
        end;
    until iBytesRead = 0;
  finally
    stream.Free;
  end;
end;
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question