Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

record lookup in qbasic

Status
Not open for further replies.

yk2002

Technical User
Jun 14, 2002
1
US
I am looking for a tool which will allow me.
1. Access immediate at any point large data files (not sequential access).
2. Create an index in qbasic 4.5 program.
3. Lookup a record in that was indexed in qbasic 4.5.

ie. let say that the file contain the following info:
last name, first name, zip, address

I like to find all names in the file matching to specific last name and zip. Also, I like to be able to access data by partial fields.


Some years back I had the utility but lost it.

Thanks for any help.
 
use
FOR a = 1 TO LOF(1)
GET #1,a,var$
if var$ = lastname THEN ...
NEXT

save them like this "LAST,FIRST,18324,POBOX1758".
Seperate them during the FOR-NEXT loop into four different strings using MID$
 
yk2000,

Create a [tt]TYPE[/tt] for your record, and open the file [tt]FOR BINARY[/tt]. I have an example that shows how you can maintain a list of deleted records, with a large block of untested code :)-)) in another thread. With any luck, this should link to it:

thread314-289593

The basic idea is that you take the length of that [tt]TYPE[/tt] (and you can't just use [tt]LEN[/tt] on the [tt]TYPE[/tt], as this will just give you 4 for some reason (perhaps it is erroneously treating it as a variable of type [tt]SINGLE[/tt]) -- you have to create a variable of that [tt]TYPE[/tt] and take the [tt]LEN[/tt] of that variable), and then you use that to calculate the offset into the file of the corresponding record on disk. For instance, if the record is 500 bytes long, then the offset of record #20 is 20 * 500 = 1000 bytes from the start of the file. Since the start of the file is offset 1, this means that record #20 is offset 1001. In general, the following formula will give you the offset on disk:
[tt]
diskOffset& = 1 + recordNumber& * LEN(recordVariable)
[/tt]
With this scheme, the first record in the file is numbered 0.

Anyway, now that you have the offset of the record, you can load the existing data into that [tt]recordVariable[/tt] with [tt]GET[/tt] and save the current data from the [tt]recordVariable[/tt] using [tt]PUT[/tt]. Here is a simple example:
[tt]
TYPE recordType
userName AS STRING * 50 ' a 50-byte record
END TYPE

DIM
recordVariable AS recordType

OPEN "database" FOR BINARY AS #1

DO
INPUT
"Record number"; recordNum&
byteOffset& = 1 + recordNum& * LEN(recordVariable)
GET #1, byteOffset&, recordVariable
PRINT "Current username: "; recordVariable.userName
PRINT "Change [y/n]? ";
DO
a$ = UCASE$(INPUT$(1))
LOOP UNTIL INSTR("YN", a$)
PRINT a$
IF (a$ = "Y") THEN
LINE INPUT
"New name: ", recordVariable.userName
PUT #1, byteOffset&, recordVariable
PRINT "Changes saved!"
END IF
PRINT
"Continue [y/n]? ";
DO
a$ = UCASE$(INPUT$(1))
LOOP UNTIL INSTR("YN", a$)
PRINT a$
LOOP UNTIL a$ = "N"

CLOSE #1
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top