Solved

Read/Save contents

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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