Problem saving 2D array into file

Hi,

I wanted to save a 2D array into a file but something
doesn't work as I expected to. Sourcecode looks as follows:
[code]
var data :array of array of byte;
p1 :file;
begin
SetLength (data,10,10);
assignfile (p1,'test.tmp');
rewrite (p1,1);
BlockWrite (p1,data,100);
closefile(p1);
end;
[/code]
When I read the data from the saved file (using BlockRead) I get
empty or random elements. I'll be appreciated for help.

Bw
Philip

Comments

  • : Hi,
    :
    : I wanted to save a 2D array into a file but something
    : doesn't work as I expected to. Sourcecode looks as follows:
    : [code]
    : var data :array of array of byte;
    : p1 :file;
    : begin
    : SetLength (data,10,10);
    : assignfile (p1,'test.tmp');
    : rewrite (p1,1);
    : BlockWrite (p1,data,100);
    : closefile(p1);
    : end;
    : [/code]
    : When I read the data from the saved file (using BlockRead) I get
    : empty or random elements. I'll be appreciated for help.
    :
    : Bw
    : Philip
    :
    The problem isn't the fact that the array is 2D, but dynamic. The address location of the variable isn't necessarily the same address location of the first element. You should write (and read) the first element. That way your code should write/read the memory of the elements and not of the dynamic array overhead.
  • It really works :D
    Thanks



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories