Search code examples
delphifilestreamdynamic-arrays

How Can I Save a Dynamic Array to a FileStream in Delphi?


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?


Solution

  • 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));