RE: RANDOM and file size

by (no login)

Hi Lisztfr,
It's clear to me you need a more customized solution to your problem than QBASIC can provide. I have some thoughts which may help you.

Firstly, note that SEEK can be used to get and set the read/write pointer's position in a file. This means you can make a note of which position you are in the file at the beginning of each line when a file is open for INPUT too.

OPEN myfile$ for INPUT AS #3
p&=SEEK(3)
LINE INPUT #3,a$
PRINT a$
SEEK #3,p&
LINE INPUT #3,a$
PRINT a$ 'this prints the same thing!

That said, the solution I recommend in your case is the scan the source file once, making a note of each lines position in the file:

OPEN myfile$ FOR INPUT AS #3

OPEN lineoffsets$ FOR OUTPUT AS #2: CLOSE #2 'destroys & truncates previous (if any exists) listing file
OPEN lineoffsets$ FOR BINARY AS #2
DO WHILE EOF(3)=0
p&=SEEK(3)
PUT #2,,p&
LINE INPUT #3,a$
numlines=numlines+1
LOOP

Now you can quickly jump to and read any line within the file:
lineiwant=5
GET #2,lineiwant*4-3,p&
SEEK #3,p&
LINE INPUT #3,a$

So how big will the line offsets file get? 4 bytes multiplied by the number of lines. So that's quite small really. You could store the offsets in an array but you'd never be able to load a file of over 16000 lines.

The above solution is only good for a reader, not an editor, but I believe it could be the best in your situation.

PS. I heard all the arguments about how RANDOM is "bad". It's rubbish. If I were making a text editor I'd use RANDOM over BINARY any day, because if you didn't you'd just complicate matters for yourself and get NO BENEFIT AT ALL. But for those who like doing it the hard way and not learning new things... eh, go back to used interrupts to access your hard disk sectors

Posted on Jul 24, 2008, 1:39 AM
from IP address 122.104.43.249

Respond to this message   

Goto Forum Home


Response TitleAuthor and Date
Ironically, I could have used RANDOM instead of BINARY for indexing in my previous example on Jul 24
Thanks i will try this on Jul 24
Size ok nice solution on Jul 24
Couldn't you have used simple Integer instead of & ? on Aug 5