How to save a dym array to a file and read it out?

chengjian
chengjian used Ask the Experts™
on
Hi

I have a dyman array, it contains a record

//Decleration
  tagEVENTMSG = packed record
    message: UINT;
    paramL: UINT;
    paramH: UINT;
    time: DWORD;
    hwnd: HWND;
  end;
  TEventMsg = tagEVENTMSG;
  {$EXTERNALSYM EVENTMSG}
  EVENTMSG = tagEVENTMSG;
EventArr:array of EVENTMSG;
//End of declearation


Now I want to mathods, code is required, thanx:

Savetofile(fn:String);
Loadfromfile(fn:String);

if you have time, give a solusion for TFileStream. although other is acceptable.

Thanx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
have fun
babeoma


uses
  SysUtils, Classes, Windows;

type
  tagEVENTMSG = packed record
    message: UINT;
    paramL: UINT;
    paramH: UINT;
    time: DWORD;
    hwnd: HWND;
  end;
  TEventMsg = tagEVENTMSG;
{$EXTERNALSYM EVENTMSG}
  EVENTMSG = tagEVENTMSG;

var
  EventArr: array of EVENTMSG;

procedure SaveToFile(fileName: string);
var
  fs: TFileStream;
  i: integer;
begin
  fs := TFileStream.Create(fileName, fmCreate);
  try
    i := Length(EventArr);
    fs.WriteBuffer(i, SizeOf(integer));
    for i := 0 to (Length(EventArr) - 1) do
    begin
      fs.WriteBuffer(EventArr[i], SizeOf(EventArr[i]));
    end;
  finally
    fs.Destroy;
  end;
end;

procedure LoadFromFile(fileName: string);
var
  fs: TFileStream;
  i: integer;
begin
  fs := TFileStream.Create(fileName, fmOpenRead);
  try
    fs.ReadBuffer(i, SizeOf(integer));
    SetLength(EventArr, i);
    for i := 0 to (Length(EventArr) - 1) do
    begin
      fs.ReadBuffer(EventArr[i], SizeOf(EventArr[i]));
    end;
  finally
    fs.Destroy;
  end;
end;
chengjian:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial