![]() |
|
More info about Random Access files - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: More info about Random Access files (/showthread.php?tid=4505) |
More info about Random Access files - PhilOfPerth - 02-24-2026 I've been trying to build a small utility to create a Random Access file from an existing text file, and had a few problems getting it to work. I have an alpha- sorted, single-element text file, and need to do a binary search to find words within it. I have got this to work , but found, after experimenting, that the Len for the file needed to be at least 2 bytes longer than the max data size. I could find no information about this in the Wiki, and reckon R/A files deserve their own spot there. Here's a (very simple) prog that demonstrates the problem, with the added 2 bytes. Code: (Select All) SW = 1040: SH = 720RE: More info about Random Access files - bplus - 02-24-2026 Each record for a Random Access file must be exactly the Len spec'd in Open statement for strings you must use fixed length strings Dim as String * some number! (not a variable) Code: (Select All) SW = 1040: SH = 720RE: More info about Random Access files - SMcNeill - 02-24-2026 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.
RE: More info about Random Access files - SMcNeill - 02-24-2026 (02-24-2026, 01:30 AM)bplus Wrote: ' random access needs fixed record lengths must use fixed strings for records @bplus Read my post above. This is wrong. RANDOM works fine with variable length strings. You just need to account for the CRLF which is attached to them as a size placeholder. RE: More info about Random Access files - bplus - 02-24-2026 "don't forget to take that CRLF into account in size" nope not in Random access files! RE: More info about Random Access files - Pete - 02-24-2026 You can get away with strings of different lengths, but the longest string cannot exceed the specified file length, minus 2-bytes. Code: (Select All)
Pete RE: More info about Random Access files - bplus - 02-24-2026 This works fine exactly 15 * 6 for LOF no need for counting CRLF$ Code: (Select All) ' Verify the random access fileRE: More info about Random Access files - SMcNeill - 02-24-2026 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)
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. RE: More info about Random Access files - bplus - 02-24-2026 LOF = 30 Code: (Select All) Open "crap" For Random As #1 Len = 10RE: More info about Random Access files - bplus - 02-24-2026 To determine number of records divide LOF() by Len = (used in Open statement). |