Type
TRecord = Record
Name: String[10];
address: String[50];
End;
TRecordFile = File Of TRecord;
Var
Form1: TForm1;
aRecordFile: TRecordFile;
aRecordFileName: String = 'c:\PersonS.dat';
Implementation{$R *.dfm}
Function RecordsSaveToFile(aFileName: String; aRecord: TRecord): Boolean;
Begin
Result:=False;
AssignFile(aRecordFile, aFileName);
FileMode := 2;
If Not (FileExists(aFileName)) Then Rewrite(aRecordFile);
Reset(aRecordFile);
Seek(aRecordFile, FileSize(aRecordFile));
Try
Write(aRecordFile, aRecord);
Result:=True;
Finally
CloseFile(aRecordFile); // Close the file when finished.
End;
End;
Function RecordsLoadFromFile(aFileName: String): Boolean;
Var
aRecord: TRecord;
aRecordFile: TRecordFile;
RecCount, i: Integer;
Begin
Result:=False;
If Not (FileExists(aFileName)) Then Exit;
AssignFile(aRecordFile, aFileName);
FileMode := 2;
Reset(aRecordFile);
Try
RecCount := FileSize(aRecordFile);
For i := 0 To RecCount - 1 Do Begin
Seek(aRecordFile, i);
Read(aRecordFile, aRecord);
Form1.Memo1.Lines.Add(aRecord.Name + '=' + aRecord.address)
End;
Result:=True;
Finally
CloseFile(aRecordFile);
End;
End;
Procedure TForm1.Button1Click(Sender: TObject);
Var
aRecord: TRecord;
i: Integer;
Begin
For i := 0 To 10 - 1 Do Begin
aRecord.Name := 'aName' + IntToStr(i);
aRecord.address := '1234asdfssdf' + IntToStr(i);
RecordsSaveToFile(aRecordFileName, aRecord); //存入文件調用
End;
End;
Procedure TForm1.Button2Click(Sender: TObject);
Begin
RecordsLoadFromFile(aRecordFileName);
End;