Board index » delphi » save/load data arrays to a file

save/load data arrays to a file

I have several datas arrays, dynamically generated from a master software.
Arrays dimensions are not fixed, and not all datas exists on each session of
the soft. For example :
I have 3 data types : Data1, Data2, Data3.
Each data type exists or not on a session.
If exist, it is a dynamical array (of integer) : Data1[0..n],...

I don't see how to save them in a single file, and more important, how to
load/read them and reaffect them to good fields.

After that, I think I would crypt the file...

 

Re:save/load data arrays to a file


Quote
"Redaction Net-Infos" <redact...@net-infos.com> wrote in message

news:9va6gi$hv7$1@wanadoo.fr...

Quote
> I have several datas arrays, dynamically generated from a master software.
> Arrays dimensions are not fixed, and not all datas exists on each session
of
> the soft. For example :
> I have 3 data types : Data1, Data2, Data3.
> Each data type exists or not on a session.
> If exist, it is a dynamical array (of integer) : Data1[0..n],...

> I don't see how to save them in a single file, and more important, how to
> load/read them and reaffect them to good fields.

> After that, I think I would crypt the file...

This one is quite easy, here's how I'd do it.

Create a TFileStream, look it up in the Delphi help for information on using
it. It's quite straight forward.

Save the number of Elements in Data1 to the Stream, i.e. Length( Data1 )
Then Loop through Data1 and save each element to the stream.

If there are no items in Data1, then still save the length, it'll be 0, so
that will tell you that there's no items when you come to load back up.

Rinse and Repeat this for Data2 and Data3, Then Free the TFileStream, and
it's saved.

To load the file, simple repeat the above process in reverse.

Hope this helps.

Colin.

Re:save/load data arrays to a file


Quote
"Redaction Net-Infos" <redact...@net-infos.com> wrote in message

news:9va6gi$hv7$1@wanadoo.fr...

Quote
> I have several datas arrays, dynamically generated from a master software.
> Arrays dimensions are not fixed, and not all datas exists on each session
of
> the soft. For example :
> I have 3 data types : Data1, Data2, Data3.
> Each data type exists or not on a session.
> If exist, it is a dynamical array (of integer) : Data1[0..n],...

> I don't see how to save them in a single file, and more important, how to
> load/read them and reaffect them to good fields.

Type
    tDataArray = array of LongInt;

procedure SaveDataToStream (aStream : tStream; Data1, Data2, Data3 :
tDataArray);

    procedure SaveOneArray (aStream : tStream; Data : tDataArray);

    var lth : LongInt;

    begin
    lth := Length (Data);
    aStream.Write (lth, SizeOf (lth));
    if lth > 0
    then aStream.Write (Data [0], lth * SizeOf (LongInt));
    end;

begin
SaveOneArray (aStream, Data1);
SaveOneArray (aStream, Data2);
SaveOneArray (aStream, Data3);
end;

procedure LoadDataFromStream (aStream : tStream; var Data1, Data2, Data3 :
tDataArray);

    procedure LoadOneArray (aStream : tStream; var Data : tDataArray);

    var lth : LongInt;

    begin
    aStream.Read (lth, SizeOf (lth));
    SetLength (Data, lth);
    if lth > 0
    then aStream.Read (Data [0], lth * SizeOf (LongInt));
    end;

begin
LoadOneArray (aStream, Data1);
LoadOneArray (aStream, Data2);
LoadOneArray (aStream, Data3);
end;

Quote
> After that, I think I would crypt the file...

Using streams the process becomes relatively simple.

SaveDataToStream (aMemoryStream, Data1, Data2, Data3);
EncryptStream (aMemoryStream, aFileStream);
. . .
DecryptStream (aFileStream, aMemoryStream);
LoadDataFromStream (aMemoryStream, Data1, Data2, Data3);

Re:save/load data arrays to a file


Quote
> Type
>     tDataArray = array of LongInt;

> procedure SaveDataToStream (aStream : tStream; Data1, Data2, Data3 :
> tDataArray);

>     procedure SaveOneArray (aStream : tStream; Data : tDataArray);

>     var lth : LongInt;

>     begin
>     lth := Length (Data);
>     aStream.Write (lth, SizeOf (lth));
>     if lth > 0
>     then aStream.Write (Data [0], lth * SizeOf (LongInt));
>     end;

> begin
> SaveOneArray (aStream, Data1);
> SaveOneArray (aStream, Data2);
> SaveOneArray (aStream, Data3);
> end;

> procedure LoadDataFromStream (aStream : tStream; var Data1, Data2, Data3 :
> tDataArray);

>     procedure LoadOneArray (aStream : tStream; var Data : tDataArray);

>     var lth : LongInt;

>     begin
>     aStream.Read (lth, SizeOf (lth));
>     SetLength (Data, lth);
>     if lth > 0
>     then aStream.Read (Data [0], lth * SizeOf (LongInt));
>     end;

> begin
> LoadOneArray (aStream, Data1);
> LoadOneArray (aStream, Data2);
> LoadOneArray (aStream, Data3);
> end;

> > After that, I think I would crypt the file...

> Using streams the process becomes relatively simple.

> SaveDataToStream (aMemoryStream, Data1, Data2, Data3);
> EncryptStream (aMemoryStream, aFileStream);
> . . .
> DecryptStream (aFileStream, aMemoryStream);
> LoadDataFromStream (aMemoryStream, Data1, Data2, Data3);

but why not make it a little more re-usable, like this.....

procedure SaveDataToStream (aStream : tStream; Data : Array of tDataArray);
var
  i : Integer;
begin
  For i := Low( Data ) to High( Data ) Do
    SaveOneArray (aStream, Data[ i ]);
end;

Then you'd call it like this.....

SaveDataToStream ( Stream, [ Data1, Data2, Data3 ] );

Of course you do the same thing to the load statement.

Colin.

Re:save/load data arrays to a file


Quote
"Colin Dawson" <N...@cdawson.tele2.co.uk> wrote in message
> Of course you do the same thing to the load statement.

The load isn't so simple to call because one can't use the open array
constructor to form the actual parameter. (The reason for this is that the
parameter has to be passed by reference so that the load can set the length
of each of the dynamic arrays.) I haven't actually tried it, but I suppose
one could declare a variable of an array of tVarRec and construct the actual
parameter before making the call. I'm not sure that the extra work is worth
it.

Other Threads