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;

  • 相关阅读:
    函数式编程二 之异常处理
    ANSI是什么编码?
    Python:解析PDF文本及表格——pdfminer、tabula、pdfplumber 的用法及对比
    文件(csv、excel、xml、html)的读取(read)和写入(write)方法——python
    [python]提取PPT中的文字(包括图片中的文字)
    spring boot druid数据源配置
    spring boot打包
    rust常用技巧
    01_spring概述
    spring boot集成swagger文档
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940679.html
Copyright © 2011-2022 走看看