[futurebasic] Still need help with Write File

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : September 1999 : Group Archive : Group : All Groups

From: scram@... (Stu Cram)
Date: Wed, 22 Sep 1999 07:44:09 -0700
Rick Brown's right on the mark about using WRITE to send a large array to a
file.

The format is:    WRITE FILE #n, adrr, numBytes

An expression like "@myArray(0)" yields the address where the array starts.
How to get the number of bytes in the array can be tedious and error prone.

The suggested method to get the number of bytes in the array involved using
how many elements the array has and multiplying that by the size of each
element. This is a pain (for me  at least) as I sometimes change the data
type of the array (poor planning I suppose), especially with strings.

The following approach seems to work well to determine how many bytes are
in an array. When the array is dimensioned, create another variable
immediately after. Then subtract their addresses to get the length of the
first array.

Here's a code sample...
    -----------------------------------------
    DIM  myArray(55,23)
    DIM  myArrayEnd.0
    '...
    addr = @myArray(0,0)
    numBytes = ABS(@myArrayEnd - addr)
    WRITE FILE #1, addr, numBytes
    -----------------------------------------

Be sure to use a similar READ FILE instruction to replace the data in
memory when needed.

This can be adapted to write a whole group of variables to a file in one
step rather than individually -- just define them one after another in the
DIMs so that they are in successive bytes of memory and calculate the
number of bytes from the first variable to extra dummy variable at the end
of the group.  I imagine this would be one way to save the state of a game
in progress and continue when you restart, for example.

- Hope this helps a bit.
  Stu Cram, Regina, Sask. CAN