01-20-2005, 12:30 AM
This program is designed to demonstrate a failing of the FB library, or possibly of FB itself. However, if there is an elegant workaround for the problem highlighted, I'm intrested in that, too.
Code:
''This is a demo program to illustrate a failing in FB/QB
CLS
OPTION BASE 0
''I'm not sure OPTION BASE really works. If I DIM something(12) as INTEGER, I don't get the values from 0 to 11, I seem to get values from 0 to 12, which amounts to 52 bytes, not the wanted 48 bytes (12 elements * 4bytes each)
TYPE pokey field = 1
a(0 to 11) AS INTEGER ''If I don't include a lower bound, is the number I specify the max index, or the actual number of elements requested as in C?
END TYPE
DIM toyInteger AS INTEGER
DIM toyString AS STRING * 12
REDIM torch(1 to 12) AS BYTE
REDIM dork(1 to 12) AS pokey
''Works, creates a basic table
FOR y = 1 TO 12
FOR x = 0 TO 11
dork(y).a(x) = x
NEXT x
NEXT y
''Works, prints out the table
FOR y = 1 TO 12
FOR x = 0 TO 11
PRINT dork(y).a(x);
NEXT x
PRINT
NEXT y
PRINT
PRINT LEN(toyInteger) ''Should print "4"
PRINT LEN(toyString) ''Should print "12"
PRINT LEN(pokey) ''Should print "48"
PRINT
''Here's the kicker: how do I get the length of the following arrays? It works for strings and single elements of arrays.
''It fails if I try to get the total length of an array.
PRINT "This element is"; LEN( dork(1).a(10) );" bytes long." ''This should also print out "4"
''Now for the coup de gras
PRINT "This array is"; LEN( dork ); " bytes long." ''This should print out "48", but it doesn't work, compiler doesn't like this.
PRINT "The torch array is"; LEN ( torch(1 to 12) ); " bytes long." ''FBC doesn't like this either.
''This highlights a slight problem with the GET function, where you have to pass to it a pointer where you want the
''data copied to. Since LEN doesn't correctly get the length for an array, it makes it hard to fill an array of similar
''structures. This is compounded by the fact that GET doesn't allow the user to specify the number of bytes to retrieve.
''This problem doesn't surface if you're using the standard datatypes singly, but it does appear when using arrays.
''Do some file opening stuff.
GET #1, , dork(1) ''This gets in 48 bytes, the size of one element of dork.
GET #1, , dork ''This fails because GET a) doesn't know how many bytes to get, and b) the compiler doesn't like the fact that the array is missing subscripts.
CLOSE #1
''However, this same general construct works in C, and probably other languages. Is there a way to get similar functionality?