Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Read/Save contents

Posted on 2001-09-14
2
Medium Priority
?
228 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 1200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Screencast - Getting to Know the Pipeline
Suggested Courses

926 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