zoukankan      html  css  js  c++  java
  • 记录数组存出到文件和从文件导入

    记录数组存入文件和从文件导入
    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;
    
  • 相关阅读:
    自学Python5.2-类和对象概念
    自学Python5.1-面向对象与面向过程
    自学Python2.1-基本数据类型-字符串str(object) 上
    自学Python2.10-跳出循环(break、continue)
    自学Python2.9-循环(while、for)
    自学Python2.8-条件(if、if...else)
    自学Python1.8-python input/print用法 格式化输出
    自学Python1.6-Centos内英文语法切换
    自学Python1.7-python变量以及类型
    自学Python1.5-Centos内python2识别中文
  • 原文地址:https://www.cnblogs.com/tulater/p/1321959.html
Copyright © 2011-2022 走看看