Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
More info about Random Access files
#11
Yes, when you use LEN with OPEN for RANDOM, the LEN is the length of the records.

But when using variable length strings, the max length of a string that you can use is that LEN - 2, as it also stores the size in front of it.

a$ = "Test"

When a$, which is a variable length string is stored, it's stored as 2-bytes for INTEGER size and then 4 bytes for the string.  Everything after that is basically just ignored.

a$(4) = "Test"

Now a$(4) is a fixed-length string of 4-bytes.  When you put it to file, it puts those 4 bytes to the file with no size in front of it.

Variable length strings have a total length limit reduced in total size by 2 bytes as they store the size with the string data.  Fixed length strings just store the data.  That's the difference you're seeing here.

So when you do something like:

OPEN "foo.txt" FOR RANDOM AS #1 LEN = 15
a$ = "123456789012345"
PUT #1, , a$

You'll get an error as what it wants to PUT is 2-bytes for length + 15 bytes for the variable length string, which is MORE than the 15 byte record size.  This is what Phil was seeing in his original post.

It's two different behaviors for two different types of data.

Variable length strings -- it puts a 2-byte integer for the size of the string, and then the string.
Fixed length strings -- it just puts the fixed length string directly to the file.

That's the difference in a nutshell.

The record size is *always* the size you specify.  But variable length strings are going to have a 2-byte size integer attached to the front of them when writing them, so the end result is they're limited to 2-bytes LESS than the total record size.

The reason for this difference?

It's so when you read back a variable length string, it doesn't automagically add all those blank spaces to the end of the string.

OPEN "crap" FOR RANDOM AS #1 LEN = 10
a$ = "1234"
a$(10) = "1234"
PUT #1, , a$
PUT #1, , 1$(10)
GET #1, 1, a$
GET #1, , a$(10)

The difference in the above is when we PRINT those two strings, they're going to look quite different.

a$ will print "1234"
a$(10) will print "1234------", which is the 1234 with 6 spaces after it.

a$ won't have those spaces; a$(10) will.   But the way to preserve the EXACT size with variable length strings *requires* you to save the size first, and that's 2-bytes.  So when you write the variable length string, it's 2-bytes for size + the data itself, which means the overall data size has to be 2 less than your total record length or you get an error.
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, 03:18 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)