March 29, 2024, 12:40:22 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Array File Operations

Started by GWS, October 25, 2008, 03:18:55 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

GWS

October 25, 2008, 03:18:55 PM Last Edit: November 06, 2008, 08:37:59 AM by GWS
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


Tomorrow may be too late ..

aurelCB

November 06, 2008, 07:03:37 AM #1 Last Edit: November 06, 2008, 08:36:23 AM by aurelCB
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

GWS

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

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



Tomorrow may be too late ..

aurelCB

November 06, 2008, 11:29:17 AM #3 Last Edit: November 08, 2008, 08:18:29 AM by aurelCB
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...