Brendon,
  You don't say whether the data in your .dat file is sorted, or how many
entries you are likely to be expecting, or whether you're developing for
Win32 or Win16.

However:
  If the records in the .dat file are unsorted and you don't want to lad the
entire file into memory, you are going to be stuck with a simple linear
search eg:

function Find( const searchText : string ) : boolean;
  var
      tmp : address;
  begin
  result := false;
  Seek(AddressFile,0);
  while (not result) and ( not EOF( AddressFile)) do
    begin
    read( addressFile,tmp);
    {if you want case-sensitivity you could use
       result := SearchText = tmp.Name}
    result := AnsiCompareText( SearchText, tmp.Name) = 0;
    {if you've found the record, then seek backwards so it can be read}
    if result then
      Seek(AddressFile,FilePos(AddressFile)-1);
    end;
  end;

This function will leave the file pointer positioned ready to read the found
record

Its a little more complex if you want to do partial matches.

  You could use:
         result = Pos( SearchText, tmp.Name ) = 1
  or for case insensitivity:
        result := Pos( UpperCase( SearchText), UpperCase(tmp.Name)) = 1
----- Original Message -----
From: <[EMAIL PROTECTED]>
To: "Multiple recipients of list delphi" <[EMAIL PROTECTED]>
Sent: Thursday, September 27, 2001 3:55 PM
Subject: [DUG]: Help with searching a .dat file


Hi all, i have a edit box and a button on my form with the find dialog
as well.
my program is a address book. which  it's data is written to a .dat
file. i have made a record type
type
  address=record
   name :string[50];
   streetaddress:string[40];
  city:string[20];
  phone:string[15];
 mobile:string[15];
email:string[40];
end;

on my form also i have 6 other edit boxes which the various info from
above is displayed in.
what i want to do is use the button(search) it searches through till
it find that record ie if i type in joe bloggs it brings up the record
for joe bloggs,
i would like some code to help me with this please.
b4 i forget
these are my other variables to go with it.
var
  AddressFile    :File Of address;
  Addressdata    :Address;
  Fname          :String;
  recsize,currec :longint;

...
   Brendon Toogood

E-Mail [EMAIL PROTECTED]
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to