"Manuel Meyer" <manuel.me
...@debitel.net> wrote in message
news:3b7414db_2@dnews...
> I am working with a multidimensional dynamic array that
> stores double-values.
> To save the data on my harddisc, I want to write it into
> a stream-object (TFileStream). What is the best way to
> do that?
> The TFileStream-class has read- and write-methods. Can
> I somehow use my array as an argument for these methods?
Multidimensional dynamic arrays may point to noncontiguous memory blocks.
You will need to write custom procedures to read and write your array. You
will also need to save the size of each dimension before the actual data.
If your array has a fixed number of dimensions then you can get away with
using nested for-loops. The innermost loop can persist an entire row of
values with one call to Stream.Write.
Please note that the following code is off the top of my head just to get
you started. It may not compile.
type
TMyArray = array of array of array of array of Double;
procedure WriteArrayToStream(const MyArray: TMyArray;
const Stream: TStream);
var
A, B, C, N: LongInt;
begin
N := Length(MyArray);
Stream.Write(N, SizeOf(N));
for A := 0 to High(MyArray) do
begin
N := Length(MyArray[A]);
Stream.Write(N, SizeOf(N));
for B := 0 High(MyArray[A]) do
begin
N := Length(MyArray[A, B]);
Stream.Write(N, SizeOf(N));
for C := 0 to High(MyArray[A, B]) do
begin
N := Length(MyArray[A, B, C]);
Stream.Write(N, SizeOf(N));
Stream.Write(@MyArray[A, B, C, 0], N * SizeOf(Double));
end;
end;
end;
end;
The loading mechanism would have a call to SetLength at each point where
length information was saved.
procedure ReadArrayFromStream(out MyArray: TMyArray;
const Stream: TStream);
var
A, B, C, NA, NB, NC, ND: LongInt;
begin
Stream.Position := 0;
Stream.Read(NA, SizeOf(NA));
SetLength(MyArray, NA);
for A := 0 to (NA - 1) do
begin
Stream.Read(NB, SizeOf(NB));
SetLength(MyArray[A], NB);
for B := 0 to (NB - 1) do
begin
Stream.Read(NC, SizeOf(NC));
SetLength(MyArray[A, B], NC);
for C := 0 to (NC - 1)) do
begin
Stream.Read(ND, SizeOf(ND));
SetLength(MyArray[A, B, C], ND);
Stream.Read(@MyArray[A, B, C, 0], ND * SizeOf(Double));
end;
end;
end;
end;
-Gary