02-25-2026, 01:50 PM
(02-25-2026, 01:46 PM)SMcNeill Wrote:Did you look at the code I posted HERE: https://qb64phoenix.com/forum/showthread...8#pid40188(02-25-2026, 01:27 PM)ahenry3068 Wrote:(02-25-2026, 01:14 PM)SMcNeill Wrote: Have you tried any of the examples that people have posted??Steve,
Variable length strings work just fine with RANDOM access files. There was *NO* error in Phil's original post. It works absolutely fine as written. He wasn't asking for help, or trying to see what was wrong with his code. He wanted to point out that it needed to be 2-bytes larger than the longest string so others would take notice of that fact for their own code.
It doesn't need to be fixed length. You need to stop repeating that because it's 100% *WRONG*. Everything in Phil's code works 100%, as is, with no changes to variable types or anything else needed.
You just need to specify a length large enough to hold all the data types. The largest string is 15 bytes -- "FABCDEFGHIJKLMN", and since Phil is using variable length data to store those strings in his RANDOM file, the record needs to be at least 17 bytes in size. (Largest record size + 2 bytes for the size to the be stored before it.)
When a variable length string is PUT or GOT in a random access file, it moves to the byte position you ask for in your GET/PUT statement, then it gets or puts the string size, then it gets or puts that many bites of data as a sting. As long as the size isn't larger than the record size - 2, then it works all fine and dandy.
Here. PLEASE TRY THIS CODE!!!!!
Code: (Select All)Data zero,one,two,three,four,five,six,seven,eight,nine,ten
Dim dta(10) As String 'see this? It's a variable length string
For i = 0 To 10
Read dta(i)
maxLength = _Max(maxLength, Len(dta(i))) 'get the max length of the strings
Next
Open "temp.txt" For Random As #1 Len = maxLength + 2 'the total record length has to be the string length +2 for the integer size
For i = 0 To 10
Put #1, i + 1, dta(i) 'let's put that data to the file with no problem!
Next
Do
Input "Give me a number from 0 to 10 (out of bounds to quit) =>"; num%
Get #1, num% + 1, anyString$ 'and let's get the data to a variable length string
Print "Your number was "; anyString$
Loop Until num% < 0 Or num% > 10
System
Variable length all the way. No fixed length strings in sight anywhere. It's RANDOM access...
And guess what??
IT *WORKS*.
ALL of this is *wrong*.
You *CAN* use a variable length string for putting. You simply need to make the record length the max length of the strings + 2-bytes for the size to be put in front of those strings. Fixed length strings are NOT a requirement for random file access. Variable length strings work just fine. You just need a record length large enough to hold the data without overflowing and causing an error.
What would happen in the case below?
DIM foo AS STRING * 10
OPEN "temp.txt" FOR RANDOM LEN = 1 'oops a typo and I left off the 0.
PUT #1, 1, foo
Welp, the string is fixed length, but it's larger than the record size. It's going to produce an error!
DIM Foo AS STRING: Foo = "1234567890" 'foo is a 10 variable length string of 10 bytes
OPEN "temp.txt" FOR RANDOM LEN = 10 'and here I've got 10 bytes for my record!
PUT #1, 1, Foo
And I still get that same error as I'm trying to put TWELVE bytes to the record instead of ten. The variable length string is 10 bytes, the size is a 2-byte integer, so it needs to be a record size of 12 or greater to hold all the info without problem.
Variable length strings work 100% fine and dandy. You just need to remember that they write 2-bytes to the random file for the size of the string, in front of the string. Your total record length needs to be largest string size + 2. Do that and you'll avoid any errors that might pop up and then you can GET and PUT your data without having to _TRIM$ those extra spaces off the right side of it every time you access it.
You are an amazing Wizard and probably have forgotten more about QB64 than I have ever known.
What you are describing as a file though is a FIXED LENGTH RECORD FILE. Not a normal text file.
The indexing scheme I created a few posts ago allows a NORMAL TEXT FILE with ANY LENGTH
STRING to be indexed and then the fixed length (LONGINT) records in the Index file point right to the
appropriate byte location in the Text file where each string starts !
I'm not even saying your code isn't useful, It most certainly is. But I'm very fond of pointing out that
there are always MANY approach's to almost any programming issue. The seperate Index file is
one such !
I'm not saying it's not a valid approach. I'm just saying that bplus is wrong in his constant assertion that RANDOM doesn't work with variable length strings. It does.
But as for what you're suggesting, try this: https://qb64phoenix.com/forum/showthread.php?tid=1451


