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.
Search for a line in BASIC?
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.
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.
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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.
Of course you could break larger files into pieces too.
Ted
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
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
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