Reputation: 113
Im creating a console app for a friend using Pascal on Delphi 7. Ive sorted adding a record and viewing them, but Im having problems in searching them. The records are stored in a .dat file. Any help would be great!
Thanks!
my code so far...
Type
BookRecord = Record
Number : Integer;
Title : String[50];
Author : String[50];
ISBN : String[13];
end;
Var
Book : BookRecord;
f : file of BookRecord ;
Procedure Add_Book;
Var
Title, Author, ISBN : String;
i : integer;
Begin
Assign (f, 'Books.dat');
reset (f);
Seek (f, filesize(f));
Book.Number := (filepos(f)+1);
Write ('Title: ');
Readln (Title);
For i := 1 to Length(Title) do
Title[i] := UpCase(Title[i]);
Book.Title := Title;
Write ('Author: ');
Readln (Author);
For i := 1 to Length(Author) do
Author[i] := UpCase(Author[i]);
Book.Author := Author;
Write ('ISBN: ');
readln (ISBN);
For i := 1 to Length(ISBN) do
ISBN[i] := UpCase(ISBN[i]);
Book.ISBN := ISBN;
write (f, Book);
Close (f);
End;
Procedure Show_All;
Begin
Assign (f, 'Books.dat');
Reset (f);
while FilePos(f) <> FileSize(f) do
Begin
Read (f,book);
Writeln ('File: ' , Book.Number);
Writeln ('Title: ' , Book.Title);
Writeln ('Author: ' , Book.Author);
Writeln ('ISBN: ' , Book.ISBN);
Writeln;
end;
Writeln; Writeln;
Center ('END OF FILE!');
readln;
Close (f);
end;
Procedure Delete_All;
Begin
Assign (f, 'Books.Dat');
Reset (f);
Seek (f,0);
Truncate (f);
Close (f);
end;
Thats basically my code so far... The Add_Book, Show_All and Delete_All Procs work great, but once Ive added some records how would I go about searching for an author?
Upvotes: 1
Views: 1475
Reputation: 613461
Since your records don't appear to be sorted by author you need to use linear search. Adapt your Show_All routine to achieve this, iterating through each record looking for the author.
If you have a large database then performance will be a problem and you should consider using a real database.
Upvotes: 2