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


Messages In This Thread
RE: More info about Random Access files - by SMcNeill - 02-24-2026, 01:31 AM
RE: More info about Random Access files - by Pete - 02-24-2026, 01:44 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: 1 Guest(s)