02-24-2026, 01:46 AM
Actually, Steve was *WRONG*. It's been too long since I'd last worked with RANDOM files and variable length strings. It needs those 2 strings, but it's not a CRLF after the data; it's the size as an integer BEFORE the data:
And *that's* the secret trick to how you use variable length strings and why they need additional two bytes to them. I knew it was related to the size of the data, but it wasn't a CRLF like my poor memory wanted to tell me. As you can see from the actual binary data, it's the size of the string as 2-bytes (INTEGER), and then the string itself.
When dealing with fixed length strings, there's no size appended to the front of the data, so the size is absolute, whereas with variable length strings, the size is +2 as it stores the length of the string before the data.
Code: (Select All)
SW = 1040: SH = 720
Screen _NewImage(SW, SH, 32)
SetFont: F& = _LoadFont("C:\WINDOWS\fonts\courbd.ttf", 20, "monospace"): _Font F&
_ScreenMove (_DesktopWidth - SW) / 2, 90
Print "Aim: to convert a serial text file"
Print "(single element, various lengths, alpha-sorted)"
Print "to Direct access (Random access) file."
Print: Print "Problem: Record length"
'prepare a dummy Sequential list
Restore
Data "ARMY","BREAKFAST","CONCORDE","DANGER","ENERGY","FABCDEFGHIJKLMN"
Dim Wrd$(6): For a = 1 To 6: Read Wrd$(a): Next
'name the R/A file
RandFile$ = "RandFile.dat"
'get longest data length 10, length of FLASHPOINT
Input "Max Data length"; MaxLength
RL = MaxLength + 2 ' Len needs to be extended by 2 bytes
RecNum = 0
'prepare R/A file
Open RandFile$ For Output As #1: Close
Open RandFile$ For Random As #1 Len = RL
' Read each word and write to random file
For a = 1 To 6: Put #1, a, Wrd$(a): Next
Close
' Verify the random access file
Print: Print "Reading records from random access file:"
Open RandFile$ For Random As #1 Len = RL
For a = 1 To 6
Get #1, a, wrd$: Print wrd$; " ";
Next
Sleep
Print
Print
Print "Now to print the actual data in this file, byte by byte."
Close
Open RandFile$ For Binary As #1
Dim a As _Unsigned _Byte 'read it a character at a time
Do Until EOF(1)
Get #1, , a
If a > 31 Then
Print Chr$(a);
Else
Print "("; a; ")";
End If
Loop
And *that's* the secret trick to how you use variable length strings and why they need additional two bytes to them. I knew it was related to the size of the data, but it wasn't a CRLF like my poor memory wanted to tell me. As you can see from the actual binary data, it's the size of the string as 2-bytes (INTEGER), and then the string itself.
When dealing with fixed length strings, there's no size appended to the front of the data, so the size is absolute, whereas with variable length strings, the size is +2 as it stores the length of the string before the data.

