How Can I Save a Dynamic Array to a FileStream in

2019-01-18 14:13发布

问题:

I have the following structures in Delphi 2009:

type
  IndiReportIndi = record
    IndiName: string;
    NameNum: integer;
    ReportIndiName: string;
  end;

var
  XRefList: array of IndiReportIndi;

where XRefList is a dynamic array.

I want to save XRefList to a FileStream. How do I do that AND include all the IndiName and ReportIndiName strings so that they will all be retrievable again when I later load from that FileStream?

回答1:

Use: http://code.google.com/p/kblib/

type
  IndiReportIndi = record
    IndiName: string;
    NameNum: integer;
    ReportIndiName: string;
  end;

  TXRefList = array of IndiReportIndi;

var
  XRefList: TXRefList;

To save whole XRefList to stream use:

TKBDynamic.WriteTo(lStream, XRefList, TypeInfo(TXRefList));

To load it back:

TKBDynamic.ReadFrom(lStream, XRefList, TypeInfo(TXRefList));


回答2:

type
  IndiReportIndi = record
    IndiName: string;
    NameNum: integer;
    ReportIndiName: string;
    procedure SaveToStream(Stream: TStream);
    procedure LoadFromStream(Stream: TStream);
  end;

type
  TXRefList = array of IndiReportIndi;

function LoadString(Stream: TStream): string;
var
  N: Integer;

begin
  Result:= '';
  Stream.ReadBuffer(N, SizeOf(Integer));
  if N > 0 then begin
    SetLength(Result, N);
//    Stream.ReadBuffer(Result[1], N * SizeOf(Char));
// fast version - see comment by A.Bouchez
    Stream.ReadBuffer(Pointer(Result)^, N * SizeOf(Char));
  end;
end;

procedure SaveString(Stream: TStream; const S: string);
var
  N: Integer;

begin
  N:= Length(S);
  Stream.WriteBuffer(N, SizeOf(Integer));
  if N > 0 then
//    Stream.WriteBuffer(S[1], N * SizeOf(Char));
// fast version - see comment by A.Bouchez
    Stream.WriteBuffer(Pointer(S)^, N * SizeOf(Char));
end;

procedure IndiReportIndi.LoadFromStream(Stream: TStream);
var
  S: string;

begin
  IndiName:= LoadString(Stream);
  Stream.ReadBuffer(NameNum, SizeOf(Integer));
  ReportIndiName:= LoadString(Stream);
end;

procedure IndiReportIndi.SaveToStream(Stream: TStream);
begin
  SaveString(Stream, IndiName);
  Stream.WriteBuffer(NameNum, SizeOf(Integer));
  SaveString(Stream, ReportIndiName);
end;

function LoadXRefList(Stream: TStream): TXRefList;
var
  N: Integer;
  I: Integer;

begin
  Stream.ReadBuffer(N, SizeOf(Integer));
  if N <= 0 then Result:= nil
  else begin
    SetLength(Result, N);
    for I:= 0 to N - 1 do
      Result[I].LoadFromStream(Stream);
  end;
end;

procedure SaveXRefList(Stream: TStream; const List: TXRefList);
var
  N: Integer;
  I: Integer;

begin
  N:= Length(List);
  Stream.WriteBuffer(N, SizeOf(Integer));
  for I:= 0 to N - 1 do
    List[I].SaveToStream(Stream);
end;


回答3:

var
  S: TStream;
  W: TWriter;
  A: array of IndiReportIndi;
  E: IndiReportIndi;
  ...
begin
  S := nil;
  W := nil;
  try 
    S := TFileStream.Create(...);
    W := TWriter.Create(S);
    W.WriteInteger(Length(A));
    for E in A do
    begin
      W.WriteString(E.IndiName);
      W.WriteInteger(E.NameNum);
      W.WriteString(E.ReportIndiName);
    end;
  finally
    W.Free;
    S.Free
  end;
end;  

To read those data you use a TReader and ReadInteger/ReadString.



回答4:

Simple TFileStreamEx to save variables

http://forum.chertenok.ru/viewtopic.php?t=4642

Work with them from 2005th year, without any problems...



回答5:

Just for the answer to be accurate:

Consider taking a look at the TDynArray wrapper, which is able to serialize any record into binary, and also to/from dynamic arrays.

There are a lot of TList-like methods, including new methods (like hashing or binary search).

Very optimized for speed and disk use, works for Delphi 5 up to XE2 - and Open Source.

See also How to store dynamic arrays in a TList?



回答6:

Tested solution:

procedure TForm1.FormCreate(Sender: TObject);
VAR Stream: TFileStream;
    R: IndyRecordIndy;
begin
 Stream:= TFileStream.Create;
 TRY
   //put rec in stream
   Stream.WriteBuffer(R, SizeOf(R));

   //restore record from stram
   Stream.Position := 0;
   Stream.ReadBuffer(R, Stream.Size);
 FINALLY
  FreeAndNil(Stream);
 END;
end



;