[futurebasic] Re: [FB] Still need help with Write File

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

From: Rick Brown <rbrown@...>
Date: Mon, 20 Sep 1999 20:00:15 -0500

Martin Fitzgibbons wrote:

> I have steps 1,2,3 in place. What I have tried to do while waiting for help is
> Save hndl& out to a file using Write,
> Append the Array data out using Print
> Save hndl2& out to a file using Write
> Before I crashed last night I think this was sort of working but it seemed a
> little inefficient.
> I thought it might be easy to combine the 3 lots of data and write it out once.

One thing will probably be more efficient is to write the Array data using WRITE
instead of PRINT.  This saves a binary image of the array onto disk, that exactly
matches the binary structure of the array in memory.

This is made easier by the fact that all the array elements are stored in
contiguous locations in memory.  So to use WRITE, you need to know (1) the
location in memory where the array starts, and (2) the total size, in bytes, of
the array.

The answer to (1) is easy.  An expression like "@myArray(0)" yields the address
where the array starts.  (If it's a 2-dimensional array, use an expression like
"@myArray(0,0)", etc.)

To get the answer to (2), you need to know how many bytes are occupied by each
element in the array, and how many array elements you want to write.  Then the
number of bytes you want to write is:

    numBytesPerElement * numElements

To figure numBytesPerElement, use the following guidelines:
* Short integer (%) numbers use 2 bytes
* Long integer (&) numbers use 4 bytes
* Strings use the smallest _even_ number of bytes that is at least as great as
their maximum dimensioned size.  Usually this is 256 bytes.
* A record element uses a number of bytes which is the sum of its constituent

To figure numElements for a 1-dimensional array, consider the index number of the
maximum element you want to save.  Then numElements just equals maxIndex + 1.  For
multi-dimensional arrays, it's a little more compex.  If you're working with
multi-dimensional arrays, let us know, and we'll give you more details.

One word of warning: if you use WRITE to put out the contents of the array, then
you will need to use READ# (not INPUT#) to get the contents back into the array
later.  Use the same numbers that you used for WRITE.

I think that this is the fastest way you can do it (i.e., three WRITE
statements).  This is very fast, and I don't think you'll notice any improvement
by trying to combine the data in memory and then using a single WRITE statement
(indeed, the extra effort to combine the data in memory might slow down the
overall process).

> Also how do you add a CHR$(13) to the end of a handle in memory (another issue)

LONG IF FN SETHANDLESIZE(h&, oldSize& + 1) = _noErr
  'Successfully increased handle's size by 1 byte.
  'Now append CHR$(13):
  POKE [h&] + oldSize&, 13
  '[couldn't increase handle's size -- deal with error]

Hope this helps.
 - Rick