please can someone help me in saving and loading its Dynamic array from a Stream
const
      iGlobHolderCount = 100;
    type
      TFiLeSpec = record
        iSize: Integer;
      end;
      TFileSpecLst = array of TFiLeSpec;
      TFiLeSpecList = record
        iMin: Integer;
        iMax: Integer;
        iCount: Integer;
        FileSpecLst: TFileSpecLst;
      end;
var
FFileSpec: array of TFiLeSpec;
FFileSpecList: array [1 .. iGlobHolderCount] of TFiLeSpecList;
                Write first the length of an array, and next the array data:
type
  TItem = Integer;
  TItemArray = array of TItem;
var
  Stream: TStream;
  Arr: TItemArray;
  L: LongWord;
begin
  Arr:= TItemArray.Create(1, 2, 3);
// To save
  Stream:= TFileStream.Create('C:\Temp\test.bin', fmCreate);
  L:= Length(Arr);
  Stream.WriteBuffer(L, SizeOf(L));
  Stream.WriteBuffer(Pointer(Arr)^, L * SizeOf(TItem));
  Stream.Free;
// To load
  Stream:= TFileStream.Create('C:\Temp\test.bin', fmOpenRead);
  Stream.ReadBuffer(L, SizeOf(L));
  SetLength(Arr, L);
  Stream.ReadBuffer(Pointer(Arr)^, L * SizeOf(TItem));
  Stream.Free;
end;
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With