Solved

Read/Save contents

Posted on 2001-09-14
2
220 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now