Hello

I have been trying to get past certein exercises in Pascal but for some reason I always run into problems that I don't understand. I have the following (synthesized)

TYPE
  Triangle = RECORD
    x, y, z : word;
    END;
  Triangles = ARRAY OF Triangle;

{more code}

  TriangleSize : integer = sizeof(Triangle);

  assign(src,'triangle.dat');
  reset(src,TriangleSize);
  size := integer(filesize(src));
  setlength(ts,size);
  view_triangle(ts[0]); readln; //SHOWS "(0, 0, 0)"
  blockread(src,ts,size,numRead);
  view_triangle(ts[0]); readln; //EXIT CODE 216
  FOR i := 0 TO size DO BEGIN
    write('i = ',i,': '); view_triangle(ts[i]);
  END;
  close(src);

I just don't understand why the array elements are unaccesible AFTER the BLOCKREAD. The docs are clear, the array is dynamic, SRC is untyped, I know there is 1 record because I have run another script that saves exactly 1 record with BLOCKWRITE. I used to have a procedure that would populate a global array but now I condensed everything to the bare minimum to find the error. Does exit code 216 mean that the dynamic array is now inaccessible? ( T.T)>

216 is an access violation. Most likely you're not allowed to write to the dynamic array like that.

Apart from that, you set the dynamic array to the filesize. It should be filesize divided by the size of a single record.

216 is an access violation. Most likely you're not allowed to write to the dynamic array like that

Apart from that, you set the dynamic array to the filesize. It should be filesize d

216... the example used in the docs uses a fixed size array but I don't see the problem of initializing the array size. The docs are clear that the buffer that holds the information has to be big enough and that is as big as it needs to be

filesize... the filesize returns the amount of RECORDS of a file, that is, the record SIZE is defined when the file is RESET(src, RECORDSIZE). Filesize returns 1 as expected, it has 1 record, the record that is written is the same triangle type

Okay. Have no help at hand, so was a guess about the size. Sorry.

Note that a fixed size array is handled differently, that's where the error is coming from. It may be possible to pass ts[0] to the blockread though (ts is a pointer, ts[0] points to the first memory address). (Or read one record at a time so you can use ts[i] in blockread, or use an old fashioned hack with a static array.)

use an old fashioned hack with a static array.

I'm going to try this and I'll report back if it works... It most surely will ^^
-- EDIT --

It works like a charm. Now I'll search for a way to generate the array in a procedure and pass it out to a dynamicly sized array. I suppose I'll have to pull out the elements and copy them to the dinamycally allocated memory one by one :(

Note that a fixed size array is handled differently

Where would I find a description of this difference? That'd be an awesome resource for me ^^

Here is a nice discussion on a similar issue.

Bookmarked and saved. Thankyou very much ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.