Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Save and Load dynamic array (https://www.delphipraxis.net/166031-save-load-dynamic-array.html)

sdean 26. Jan 2012 13:21

Delphi-Version: 7

Save and Load dynamic array
 
Hi i've this Class , please can someone help me in saving and loading its Dynamic array from a Stream


Delphi-Quellcode:
const
iGlobHolderCount =100;
type
 TFiLeSpec = record
  iSize : Integer;
 end;
 TFileSpecLst = array of TFiLeSpec;

TFiLeSpecList = record
  iMin          :Integer;
  iMax          :Integer;
  iCount        :Integer;
  FileSpecLst   :TFileSpecLst;
 end;
 TFileChecker = class
 Function LoadFromFile(AFileName : string) : boolean;
 Function SaveToFile(AFileName : string) : boolean;
private
FFileSpec   : array of TFileSpec;
FFileSpecList : array[1..iGlobHolderCount] of TFileSpecList;
function GetCount: integer;
public
constructor  Create;
destructor Destroy; override;
property iCount : integer read GetCount;
end;


{ TFileChecker }

constructor TFileChecker.Create;
begin
FFiLeSpec:= nil;
end;

destructor TFileChecker.Destroy;
var
 i : integer;
begin
 FFileSpec:= nil;
 for i := 1 to iGlobHolderCount do
  FFileSpecList[i].FileSpecLst := nil;
  inherited;
end;

function TFileChecker.GetCount: integer;
var
 x, i : integer;
begin
Result:=0;
 x := 0;
 for i := 1 to iGlobHolderCount do
 x:=x+ Length(FFileSpecList[i].FileSpecLst);
 Result := Length(FFileSpec)+x;
end;


function TFileChecker.LoadFromFile(AFileName: string): boolean;
var
iStream:TMemoryStream;
i:Integer;
begin
Result:=False;
SetLength(FFileSpec, 0);

iStream:= TMemoryStream.Create;
Try
iStream.LoadFromFile(AFileName);
// how it can be loaded from the Stream

Result:=True;
finally
iStream.Free;
end;

end;

function TFileChecker.SaveToFile(AFileName: string): boolean;
var
iStream:TMemoryStream;
i:Integer;
begin
Result:=False;
iStream:= TMemoryStream.Create;
Try
// how it can be saved to the Stream
iStream.SaveToFile(AFileName);
Result:=True;
finally
iStream.Free;
end;

end;
many thanks

Klaus01 26. Jan 2012 14:12

AW: Save and Load dynamic array
 
Hi,

it might work in this way (not tested).
SaveToFile works similar.

Delphi-Quellcode:
function TFileChecker.LoadFromFile(AFileName: string): boolean;
var
  iStream:TMemoryStream;
  buf: TFileSpec;
  i:Integer;
begin
  Result:=False;
  SetLength(FFileSpec, 0);

  iStream:= TMemoryStream.Create;
  Try
    iStream.LoadFromFile(AFileName);
    setLength(FFileSpec,iStream.size div SizeOf(TFileSpec));
    iStream.seek(0,soFromBeginning);
    i := 0;
    while iStream.Position < iStream.size do
       begin
          iStream.ReadBuffer(buf,sizeOf(TFileSpec));
          FFileSpec[i] := buf;
          inc(i);
       end;
    Result:=True;
  finally
    iStream.Free;
  end;

end;
Best regards
Klaus

himitsu 26. Jan 2012 14:33

AW: Save and Load dynamic array
 
Delphi-Quellcode:
    setLength(FFileSpec,iStream.size div SizeOf(TFileSpec));
...
    while iStream.Position < iStream.size do
Ist die Datei nicht ganz in Ordnung, dann bekommst du (hoffentlich) eine Zugriffsverletzung, denn
Delphi-Quellcode:
div
rundet ab und
Delphi-Quellcode:
 <
rundet auf.
Ein
Delphi-Quellcode:
for i := 0 to High(FFileSpec) do
wäre dann schon besser.

sdean 26. Jan 2012 18:24

AW: Save and Load dynamic array
 
Thank you all , But what about

Delphi-Quellcode:
FFileSpecList : array[1..iGlobHolderCount] of TFileSpecList;

DeddyH 26. Jan 2012 19:08

AW: Save and Load dynamic array
 
You should use a code-formatter. Here is the result of the built-in one of Delphi XE:
Delphi-Quellcode:
const
  iGlobHolderCount = 100;

type
  TFiLeSpec = record
    iSize: Integer;
  end;

  TFileSpecLst = array of TFiLeSpec;

  TFiLeSpecList = record
    iMin: Integer;
    iMax: Integer;
    iCount: Integer;
    FileSpecLst: TFileSpecLst;
  end;

  TFileChecker = class
    Function LoadFromFile(AFileName: string): boolean;
    Function SaveToFile(AFileName: string): boolean;
  private
    FFileSpec: array of TFiLeSpec;
    FFileSpecList: array [1 .. iGlobHolderCount] of TFiLeSpecList;
    function GetCount: Integer;
  public
    constructor Create;
    destructor Destroy; override;
    property iCount: Integer read GetCount;
  end;

{ TFileChecker }

constructor TFileChecker.Create;
begin
  FFileSpec := nil;
end;

destructor TFileChecker.Destroy;
var
  i: Integer;
begin
  FFileSpec := nil;
  for i := 1 to iGlobHolderCount do
    FFileSpecList[i].FileSpecLst := nil;
  inherited;
end;

function TFileChecker.GetCount: Integer;
var
  x, i: Integer;
begin
  Result := 0;
  x := 0;
  for i := 1 to iGlobHolderCount do
    x := x + Length(FFileSpecList[i].FileSpecLst);
  Result := Length(FFileSpec) + x;
end;

function TFileChecker.LoadFromFile(AFileName: string): boolean;
var
  iStream: TMemoryStream;
  i: Integer;
begin
  Result := False;
  SetLength(FFileSpec, 0);

  iStream := TMemoryStream.Create;
  Try
    iStream.LoadFromFile(AFileName);
    // how it can be loaded from the Stream

    Result := True;
  finally
    iStream.Free;
  end;

end;

function TFileChecker.SaveToFile(AFileName: string): boolean;
var
  iStream: TMemoryStream;
  i: Integer;
begin
  Result := False;
  iStream := TMemoryStream.Create;
  Try
    // how it can be saved to the Stream
    iStream.SaveToFile(AFileName);
    Result := True;
  finally
    iStream.Free;
  end;

end;
Well, there are dynamic elements in your static array which also contain dynamic elements themselves. The only way to store/load such kind of data is IMO an additional field which stores/retrieves the length of the dynamic array(s). Let' s say your TFiLeSpecList contains 10 FileSpecLst. So you must save the number 10. Now for each FileSpecLst the number of containing FiLeSpec has also to be stored, so you can jump step by step from one record to the other, read them and go to the next FileSpecLst. When 10 is reached, the next TFiLeSpecList can be processed.


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:42 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz