11-13-2023, 12:52 AM
(11-13-2023, 12:38 AM)eoredson Wrote: I need to know because I am using the following method to read/write a fixed length UDT array string to a random record in a file:
Code: (Select All)' define an array as string.
Type Datatype
Array As String * 16
End Type
Dim DataRecord As Datatype
Dim Array(8) As Integer
Open "datatest.dat" For Random As #1 Len = Len(DataRecord)
' store array.
For L = 1 To 8
Array(L) = L
Next
' put array into UDT string.
For L = 1 To 8
Mid$(DataRecord.Array, (L - 1) * 2 + 1, 2) = MKI$(Array(L))
Next
' put 10 records into file.
For X = 1 To 10
Put 1, X, DataRecord
Next
' get random record from file.
X = Int(Rnd * 10 + 1)
Get 1, X, DataRecord
' get array from UDT string.
For L = 1 To 8
M = CVI(Mid$(DataRecord.Array, (L - 1) * 2 + 1, 2))
Print M;
Next
End
Why go through such a complicated setup? Why not just use an array directly?
Code: (Select All)
DIM DataRecord(1 TO 8) AS INTEGER
OPEN "datatest.dat" FOR RANDOM AS #1 LEN = 16
FOR j = 1 TO 10
FOR l = 1 TO 8
count = count + 1
DataRecord(l) = count 'set the data
NEXT
PUT 1, j, DataRecord() '10 records from 1 to 80
NEXT
FOR l = 1 TO 8
DataRecord(l) = 0 'clear the old data
NEXT
x = INT(RND * 10 + 1)
PRINT "Fetching record #"; x
PRINT "Which starts at"; (x - 1) * 8 + 1;
PRINT "and ends at"; x * 8
GET 1, x, DataRecord() 'get the random record
FOR i = 1 TO 8
PRINT DataRecord(i) 'print the array
NEXT