程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 記錄數組存出到文件和從文件導入

記錄數組存出到文件和從文件導入

編輯:Delphi

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;


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved