我使用Delphi 7中的Pascal爲朋友創建了一個控制檯應用程序。我已經排序添加記錄並查看它們,但是Im在搜索時遇到問題。記錄存儲在.dat文件中。任何幫助將是偉大的!在pascal中搜索未記錄的文件記錄(Delphi 7)
謝謝!
到目前爲止我的代碼...
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;
那基本上是到目前爲止我的代碼...的Add_Book,SHOW_ALL和DELETE_ALL特效工作的偉大,但一旦伊夫添加一些記錄我怎麼會去尋找一位作家?
你需要更具體。你有什麼樣的問題?你如何閱讀記錄? –
由於您對記錄進行了排序,因此您最快的搜索將使用二分查找。除此之外,我會猜測。 –
編輯:固定標題。 –