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!

Searching a saved file

Status
Not open for further replies.

jgillette82

IS-IT--Management
Jan 6, 2002
3
US
Ok, here is the problem, i have this program that saves some info. Later, i will need to search by the person's name, but i don't know how to search the saved info for the name. If anyone has any ideas, that would be helpful. Thanks.

J.G.
 
It depends on how the data is saved. Is it in a UDT with records saved to a random file using PUT #?
[tt]
TYPE MyRecord
LastName AS STRING * 64
FirstName AS STRING * 64
IOU AS DOUBLE
END TYPE
[tt]
Or perhaps saved sequentially to a file opened for output or append using WRITE # or PRINT #.

In either case, you need to loop through the records until you find the name or a combination of last and first names.

If you are simply looking for the first occurance of a string (say "Bob Smith") in a file, you can read the file into a larger string and search with INSTR.
[tt]
ff = FREEFILE
OPEN "MYFILE.QAZ" FOR BINARY AS #ff
GE$ = STRING$(LOF(ff), 32)
GET #ff, 1, GE$
CLOSE #ff
BobIs = INSTR(GE$, "Bob Smith")
IF BobIs > 0 then
PRINT "Found Bob Smith at file position" ; BobIs
ELSE
PRINT "Can't find Bob Smith"
END IF
[tt]
Naturally, if the file is larger than 32767 bytes long, you will have to find a smarter approach.

All of this depends on how the data is saved and what you are trying to accomplish.
VCA.gif
 
Well, if you want i can e-mail you my program, i'm not sure how to do this. E-mail me at jonathan_ku@hotmail.com and i'll e-mail you my code. Thanks

Jonathan
 
as Alt255 pointed out, it depends on how you saved the information. What command did you use to do this (WRITE, PUT, PRINT, OUT....)?
 
Here is segment of code that saves the info.


SUB putfile (file$(), currentfile, licenumb)
DIM F$(10)
OPEN "R", 1, "DOGFILE.DAT", 354
FOR A = 1 TO 10: FIELD 1, (A - 1) * 35 AS NULL$, 35 AS F$(A): NEXT
FIELD 1, 350 AS NULL$, 4 AS NUM$
A = LOF(1) / 354 + 1
IF currentfile = 0 THEN currentfile = A
FOR A = 1 TO 10: LSET F$(A) = file$(A): NEXT
LSET NUM$ = MKI$(licenumb)
PUT 1, currentfile
CLOSE 1
END SUB
 
jgillette82, I'm not following your code very well. It doesn't appear to be written in Qbasic.

What version of BASIC are you using?
VCA.gif
 
open the same file as random with same filesize
field it as 10 variables plus the 4
get a record
compare the name against the 10 variables
increment the record number and jump back to get a record.

GWBASIC without the line numbers. Ed Fair
efair@atlnet.com

Any advice I give is my best judgement based on my interpretation of the facts you supply.

Help increase my knowledge by providing some feedback, good or bad, on any advice I have given.

 
since you are using PUT to record the file it should be easy just make a loop (use Do-Loop) then keep GETing the variables one at a time and search for a match using
IF x = z THEN
 
In my suggestion I goofed
set record start
open the file
get the record
field it(field #1,35 as a$,35 as b$...35 as j$,4 as k$
do the comparison
increment the record number
goto the get Ed Fair
efair@atlnet.com

Any advice I give is my best judgement based on my interpretation of the facts you supply.

Help increase my knowledge by providing some feedback, good or bad, on any advice I have given.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top