This is because you're printing a variable length string of data.
OPEN "foo.txt" FOR RANDOM AS #1 LEN = 15 <-- this says we're going to put data that is 15 characters *EXACTLY* into that random file
PUT #1, 1,"Apple" <-- This is *ONLY* 5 characters.
GET #1, 1, a$ <-- But we're supposed to be putting and getting 15 characters....
PRINT a$ <-- What do you expect this to print??
If the above only grabbed 15 characters, it would print "Apple__________" where my underscores are 10 spaces instead.
But that's not what we want, so for variable length strings, it adds a length to the beginning of the string when it prints them. (As INTEGER data.)
PUT #1, 1, "Apple" <-- This actually puts the integer size + "Apple" and then the rest is nothing.
By writing the data with size at the beginning of the string, it allows your variable length string to be used and not append those extra spaces to it like it would if you DIM a AS STRING * 15.
QB45 and QB64 has been doing this forever and ever, but you're right; it could probably use a little better documentation on the subject. When one is working with RANDOM access and putting variable length strings, don't forget to take that integer size into account in size. It's there, just as you've discovered above.
OPEN "foo.txt" FOR RANDOM AS #1 LEN = 15 <-- this says we're going to put data that is 15 characters *EXACTLY* into that random file
PUT #1, 1,"Apple" <-- This is *ONLY* 5 characters.
GET #1, 1, a$ <-- But we're supposed to be putting and getting 15 characters....
PRINT a$ <-- What do you expect this to print??
If the above only grabbed 15 characters, it would print "Apple__________" where my underscores are 10 spaces instead.
But that's not what we want, so for variable length strings, it adds a length to the beginning of the string when it prints them. (As INTEGER data.)
PUT #1, 1, "Apple" <-- This actually puts the integer size + "Apple" and then the rest is nothing.
By writing the data with size at the beginning of the string, it allows your variable length string to be used and not append those extra spaces to it like it would if you DIM a AS STRING * 15.
QB45 and QB64 has been doing this forever and ever, but you're right; it could probably use a little better documentation on the subject. When one is working with RANDOM access and putting variable length strings, don't forget to take that integer size into account in size. It's there, just as you've discovered above.

