Search for a line in BASIC?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
AlanD
Newbie
Posts: 2
Joined: Sat Jan 01, 2011 5:08 pm

Search for a line in BASIC?

Post by AlanD »

Hi,

Is there a way to have QuickBASIC/VisualBASIC search for a line in a file instead of using individual INPUT #(Number), Variable statments for the line sequentially?

I'm not limited to QB4.5, I have QBasic 1.0, QuickBASIC 4.5, QuickBASIC Professional Development System 7 (7.1 I think.) and VisualBASIC 1.0 for MS-DOS

Thanks,
Alan D.
Theunis
Coder
Posts: 21
Joined: Sun Oct 17, 2010 5:41 am

Post by Theunis »

You must open the file as BINARY and then use SEEK to jump to a specific place in a file or a record.
If you are using a sequential file where the records are of variable length then there is no way to accurately calculate where a record starts.
If the records are of fixed length then you merely have to multiply the record number with the length of the record and then use SEEK to jump to the beginning of that record. This of course works fine for Random files where the length of the record is known but it is a useless exercise because with a RANDOM file it is easier to manipulate individual records and it is a more structured programming style.

The only other way I know to use SEEK in a sequential file of variable length is to write an index for the sequential file which will record the start position and the length of each record. This index is then read and with SEEK you can jump directly to that record.

Of course you can convert the sequential file to a RANDOM file. As the Random file is of fixed length it will naturally be longer than the sequential file.
Last edited by Theunis on Sun Jan 02, 2011 7:50 am, edited 1 time in total.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

If you are looking for text, use INSTR(text$, "String you are looking for..."). It will return an integer value designating the first character position when it finds a match.

You can open a text file and get the whole file as one string up to 32,000 characters.

Code: Select all

OPEN file$ FOR APPEND AS #1 'check that file exists before using INPUT
size& = LOF(1)
CLOSE #1
IF size& THEN
   OPEN file$ FOR INPUT AS #1  
   IF size& <= 32700 THEN  ' FORUM post problem!
   text$ = INPUT$(size&, 1)
   DO
      position& = INSTR(text$, search$)
      IF position& = 0 THEN EXIT DO
      PRINT "Found at:"; position
   LOOP
   ELSEIF size& > 32700 THEN PRINT "file too large!"
   END IF
ELSE KILL file$ 'delete file if empty
END IF
CLOSE #1
Of course you could break larger files into pieces too.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
AlanD
Newbie
Posts: 2
Joined: Sat Jan 01, 2011 5:08 pm

Post by AlanD »

Thank you very much burger2227 and Theunis. very helpful. :D
Post Reply