home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!news.claremont.edu!ucivax!news.service.uci.edu!nntpsrv
- From: answer@orion.oac.uci.edu (Reference)
- Subject: B-search
- Nntp-Posting-Host: orion.oac.uci.edu
- Message-ID: <2AA5120A.2223@news.service.uci.edu>
- Newsgroups: comp.lang.pascal
- Organization: University of California, Irvine
- Lines: 71
- Date: 2 Sep 92 19:12:10 GMT
-
- Hi,
-
- Being more or less self taught at TP, I figure I sometimes do stuff the hard
- way, out of ignorance. Most of the time my way works fine, however I'm
- now confronted where my way doesn't work.
-
- What I've got here is a binary search procedure that's supposed to read a list
- name from a sorted binary file, and then return the e-mail address for that
- name. It works great, except for the last record, which it is unable to
- read (say the last name "Zebra" for instance).
-
- I know that the person "Zebra" is there, I can see it when I read the binary
- file, I just can't get to it when I do my binary search.
-
- The code listed below always thinks the second to last record of the sorted
- file is the last record. I could make sure I pad the file with some dummy
- name such as "ZZZZZZ", but that's pretty lame.
-
- Any suggestions?
-
- Tony
-
- Anthony Toyofuku
- Main Library.
- University of California,
- Irvine
-
-
- ----------------------------CUT HERE------------------------------
-
-
-
-
- type
- string15 = string[15];
- string40 = string[40];
- AddRecord = record
- LastName : string15;
- FirstName : string15;
- Add : string40;
- end;
- FileBin = file of AddRecord;
-
- Procedure BSearch(WantedAddress : string;
- var Person : AddRecord; var Found : boolean);
-
- var
- First, Last, Middle : byte;
- File1 : Filebin;
-
- begin
- Assign(File1, FILENAME); { FILENAME is declared as a }
- reset(File1); { constant "address.bin". }
- First := 1;
- Last := FileSize(File1);
- Found := false;
- while (First <= Last) AND (NOT Found) do
- begin
- Middle := (First + Last) div 2;
- seek(File1, Middle - 1); { I think my problems lie here }
- read(File1, Person);
- if WantedAddress = Person.LastName then
- Found := true
- else if WantedAddress < Person.LastName then
- Last := Middle - 1
- else
- First := Middle + 1;
- end;
- close(File1);
- end;
-
-