[futurebasic] Re: Best way to save data to files

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : May 1998 : Group Archive : Group : All Groups

From: "Deepesh Letap" <DLetap@...>
Date: Wed, 27 May 1998 19:49:21 +0100

Andy

I regularly read and write multi dimensioned arrays without problems.
Principally, in the global section you must declare the array with the
following as an example:

DIM MyVarArray1Name%(3,4,5,6)
DIM MyVarArray2Name!(3,4,5,6)
DIM MyVarArray3Name#(3,4,5,6)
DIM MyVarArray4Name#(3,4,5,6)
END GLOBALS

You can define the block sizes by multiplying the array size for each
dimension (adding 1 to allow for the zero) to get the total number of
elements. This is then multiplied by the number of bytes per element:

BlockSizeVar1%=((3+1)*(4+1)*(5+1)*(6+1))*2
BlockSizeVar2%=((3+1)*(4+1)*(5+1)*(6+1))*4
BlockSizeVar3%=((3+1)*(4+1)*(5+1)*(6+1))*8
BlockSizeVar4%=((3+1)*(4+1)*(5+1)*(6+1))*8

Then, having assigned values, you can save the block using:

OPEN "O",1,"MyFileName",,VolRefNum%
WRITE FILE #1,VARPTR(MyVarArray1Name%(0,0,0,0)),BlockSizeVar1%
WRITE FILE #1,VARPTR(MyVarArray2Name!(0,0,0,0)),BlockSizeVar2%
WRITE FILE #1,VARPTR(MyVarArray3Name#(0,0,0,0)),BlockSizeVar3%
WRITE FILE #1,VARPTR(MyVarArray4Name#(0,0,0,0)),BlockSizeVar4%
CLOSE #1

When you need to retrieve the data, simply read using:

OPEN "I",1,"MyFileName",,VolRefNum%
READ FILE #1,VARPTR(MyVarArray1Name%(0,0,0,0)),BlockSizeVar1%
READ FILE #1,VARPTR(MyVarArray2Name!(0,0,0,0)),BlockSizeVar2%
READ FILE #1,VARPTR(MyVarArray3Name#(0,0,0,0)),BlockSizeVar3%
READ FILE #1,VARPTR(MyVarArray4Name#(0,0,0,0)),BlockSizeVar4%
CLOSE #1

In theory, you could simply replace this with less disk access using:

TotalBlockSize%= BlockSizeVar1%+ BlockSizeVar2%+ BlockSizeVar3%+
BlockSizeVar4%
OPEN "I",1,"MyFileName",,VolRefNum%
READ FILE #1,VARPTR(MyVarArray1Name%(0,0,0,0)),TotalBlockSize%
CLOSE #1

However, the latter assumes all array variables are created in the order
that they are DIMmed. To be safe, I have not made this assumption although
all versions of FB to date have held true.

Hope this helps,

Deep





>What is the best/fastest method for saving 2 dimensional arrays to
>files. I've tried WRITE FILE / READ FILE but it doesn't seem to like my
>arrays  - DIM gData(40,600) - when I come to read them back (they write
>out ok). I've tried reading in the data back to handle and then
>BLOCKMOVE the data but FB must detect this as potentially dangerous and
>halts (without an error message). My current method of PRINT / INPUT is
>really very slow. I suppose I could copy all the data into a temporary
>one dimensional array and copy that... I haven't tried that... Any other
>suggestions?