views:

17

answers:

2

this segment in my program first ads a customer to a textfile (declared in public variables) and saves it to a texfile. onbutton1click is the procudere to search the string thats in the editbox and return the relevant customer details to memo. the add customer works fine and adds to textfile, however when i search it returns nothing onto the memo, just the memo caption, memo1. any way i can resolve this? sorry im a newb to this.

  procedure TForm2.btnsaveClick(Sender: TObject);

  begin
     cusfname:= edit1.text ;
     cuslname:= edit2.text;
     adress:= edit3.text;
     phone:= edit4.text;
     password:= edit5.Text;
     AssignFile(F, 'Data.txt');
     append(F);
     WriteLn(F, cusfname);
     WriteLn(F, cuslname);
     WriteLn(F, adress);
     WriteLn(F, phone);
     WriteLn(F, password);
     CloseFile(F);
    end;

   procedure TForm2.Button1Click(Sender: TObject);

   var

     SearchFile : Textfile;
     found: boolean;
     search: string;

    begin
    search := edit1.text;
    Assignfile(SearchFile, 'data.txt');
    Reset(SearchFile);
    found:= false;
    repeat
      found:= search = phone
    until eof(searchfile) or found;
    if found then
    memo1.append(phone);
    memo1.append(cusfname);
    memo1.append(adress);
    if not found then 
      showmessage('member not found');
   end;                           
A: 

wonder where are the read statements? In the write function you have Write() statements, but in the reading code no read() statements?

Marco van de Voort
beginreset(searchfile); Assignfile(SearchFile, 'data.txt'); Reset(SearchFile); found:= false; search := edit1.text ; repeat read(searchfile, phone) ; read(searchfile, cusfname); read(searchfile, adress); found:= search = phone until eof(searchfile) or found; if found then memo1.append(phone); memo1.append(cusfname); memo1.append(adress); closefile(searchfile) ; if not found then showmessage('member not found');
haz
Why do you use read() here and writeLN() when you write?
Marco van de Voort
A: 

In your code you do not read() from file. In other similar question (probably your own): http://stackoverflow.com/questions/4013629/runerror102-file-not-assigned there is read(). But I think you should use readln(), or even better use TStringList class from Classes unit with its LoadFromFile() method and Lines property.

Michał Niklas