Using QBASIC's indexing would be faster/"better"

by (no login)

QBASIC has sort very effective indexing methods when working with files open for RANDOM reading & writing. First, you will need to convert the existing text file into a RANDOM access file, which is stored in a special format.
Here's how to perform the conversion:

OPEN mynewfilename$ for RANDOM AS #2 LEN=1000 'change 1000 to the maximum line width in characters required, or just a big number if you're not sure!
OPEN myfilename$ for INPUT AS #1
DO WHILE EOF(1)=0
LINE INPUT #1,theline$
PUT #2,,theline$
numlines=numlines+1
LOOP
CLOSE #1

Now that the file is converted, access it in the following way:

GET #2,5,gotline$ 'stores the contents of line 5 into gotline$
GET #2,numlines,gotline$ 'stores the contents of the last line into gotline$
savethis$="hello"
PUT #2,2,savethis$ 'overwrites line 5 with the string hello
numlines=numlines+1
PUT #2,numlines,savethis$ 'add a new line with the string hello

The file has been edited, so now it will be saved back into its original format:

OPEN mysavefilename$ for OUTPUT AS #1
FOR i=1 to numlines
GET #2,i,myline$
PRINT #1,myline$
NEXT
CLOSE #1

Posted on Jul 23, 2008, 5:59 AM
from IP address 122.104.43.249

Respond to this message   

Goto Forum Home


Response TitleAuthor and Date
it certainly would be fastermennonite on Jul 23
 Random Files have no advantages over binary files except... on Jul 23
  i've always assumed that if your project is large enough, you'll need binary anywaymennonite on Jul 23
   Yes, as would I...but this is to say..... on Jul 23
    naturally, i disagreemennonite on Jul 23
     Here's another definition of random file. on Jul 23
      Re: Here's another definition of random file.Anonymous on Jul 23
       ahh, but ... on Jul 23
        i wish you could show me how that's truemennonite on Jul 23
         Elementary my dear menn ;) always wanted to say that. on Jul 23
          * 30$ faster? I guess time really is moneyqbguy on Jul 23
           LOL I thought that was a given ;). lol. on Jul 23
            * More than two LOLs in one post = need to go in the Laughter ForumForum Police on Jul 23
            LOL My bad ;).... on Jul 23
             Please move the post above to the appropriate forum at...Note to Pete on Jul 23
              ROFLMAO.... on Jul 23
               * I hereby declare this thread as HIJACKED! Thanks folks on Jul 23
                *yes, this has gone from how to write the op's program to how to write the op's programmennonite on Jul 24
                Not sure what hijack means to you but I don't see it here. on Jul 24
                 * I don't wanna ride on a plane with me either :-D on Jul 24
it works...:) on Jul 23
 Blank Lines are not records. 0 length versus 100 on Jul 23
  that certainly is a point in its favormennonite on Jul 23
   For this job, RANDOM may be overkill on Jul 23
    well that's the thing, i think it usually ismennonite on Jul 23
     * TYPE works better for me. Just show them that instead. on Jul 23
      I'm creating an article on the FIELD statement.... on Jul 23
       *The program MONEY.BAS that came with QBasic uses FIELD (I have a copy if you want one). on Jul 23
        * IF it's the one that originally came with QBasic, I want it ;) please. on Jul 23
         *Check your mail. on Jul 23
          *Bob, I don't have the Money.Bas either. Could you send it? on Jul 23
           *On the way. on Jul 23
       While your at it. ADD a FAQ for file access modes too. on Jul 23
        i didn't think the purpose of the forum was to write faq's all the timemennonite on Jul 24
         *Your right! You DON'T think.........you twitter about nothing on Jul 24
         adult on Sep 8
  Anyway, disk space is an issue on Jul 23
   * I sure hope not! You won't be programming long if it is. on Jul 23
    This is supposed to run on an vintage palmtop on Jul 23
     use sequential thenmennonite on Jul 23
     * My QB folder is over 300 MB alone. on Jul 23
 RE: RANDOM and file size on Jul 24
  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