IonicWind Software

Creative Basic => General Questions => Topic started by: GWS on October 25, 2008, 03:18:55 PM

Title: Array File Operations
Post by: GWS on October 25, 2008, 03:18:55 PM
This is not too clear from the documentation, but as Paul explained a few years ago,
if you use a Binary File (BFILE), you can read or write an entire array in one shot.

If you use a subscript with the array variable the reading/writing takes place at that location in the array.

Short pseudo code:


DEF a[100]:int
DEF b:BFILE

if OPENFILE(b......
WRITE b,a[0]: Rem writes all 100 elements of a
READ b,a[0]: REM reads all 100 elements of a
                      ...


If you need to read/write a single element, use a temporary variable and copy to the array later.

best wishes, :)

Graham


Title: Re: Array File Operations
Post by: aurelCB on November 06, 2008, 07:03:37 AM
How translate this vb code to Creative code, is the same thing like you say Graham,with arrays?
And how write this becose that is hexadecimal values?
Sub InitDOSHeader()
    AddLinkByte &H4D, &H5A, &H80, &H0, &H1, &H0, &H0, &H0, &H4, &H0, &H10, &H0, &HFF, &HFF, &H0, &H0
    AddLinkByte &H40, &H1, &H0, &H0, &H0, &H0, &H0, &H0, &H40, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0
    AddLinkByte &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0
    AddLinkByte &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H80, &H0, &H0, &H0
End Sub
Title: Re: Array File Operations
Post by: GWS on November 06, 2008, 09:23:24 AM
Hmm .. a bit tricky that one ..

It appears to be adding bytes to an array LinkCode[] with a view to writing the array at some point using VB code :

Open sFile For Binary As #1
For i = 1 To UBound(LinkCode)
Put #1, , LinkCode(i)
Next i
Close #1
InfMessage "application compiled. " & vbCrLf & _
Passes & " pass(es). " & UBound(LinkCode) & " bytes written."

http://www.vbforums.com/showpost.php?p=2192871&postcount=703 (http://www.vbforums.com/showpost.php?p=2192871&postcount=703)

So it looks very similar. If the array is set up as -  Def LinkCode[] as Char, you could load the Hex values directly, since the largest value will be &HFF which is 255.

I don't know if the maximum size of the array is known, or if it can be worked out.
They use an adjustable sized array using REDIM.  We don't have that feature, but an array size can be set dynamically at run time using:

DEF sizex,sizey:INT
... Calculate the sizes ...
DEF dynamic[sizex,sizey]:INT

That can only be done once though.

Then the array can be written out either byte by byte as they did above, or all in one go as - Write b, LinkCode[0] as in Paul's example.

What happens to the data later in the process I don't know ..

best wishes, :)

Graham



Title: Re: Array File Operations
Post by: aurelCB on November 06, 2008, 11:29:17 AM
This code is part of linker of Libry Compiler-small compiler wich compile directly to machine code.
Libry is completly written in VB 6.0.
And what is this UBound?
Hmm ... like you say this is tricky stuff...