Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
More info about Random Access files
#8
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:

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.
Reply


Messages In This Thread
RE: More info about Random Access files - by Pete - 02-24-2026, 01:44 AM
RE: More info about Random Access files - by SMcNeill - 02-24-2026, 01:46 AM
RE: More info about Random Access files - by Pete - 02-24-2026, 05:13 AM
RE: More info about Random Access files - by Jack - 02-24-2026, 02:41 PM
RE: More info about Random Access files - by Pete - 02-24-2026, 05:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Embedding and Extracting MANY files ! ahenry3068 20 1,500 11-15-2025, 10:19 AM
Last Post: ahenry3068
  random maze map. math help pmackay 4 553 08-10-2025, 11:22 AM
Last Post: pmackay
  Random Number Generator pmackay 14 1,231 07-30-2025, 12:56 PM
Last Post: SMcNeill
  generating a random number in the full range of that number? (Integer, Long) madscijr 2 643 05-01-2025, 09:11 PM
Last Post: madscijr
  program that stitches together a bunch of image files into one giant poster? madscijr 15 2,291 10-24-2024, 06:08 PM
Last Post: madscijr

Forum Jump:


Users browsing this thread: 2 Guest(s)