加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何将INI部分分配给delphi 7中的记录

发布时间:2020-12-15 09:05:49 所属栏目:大数据 来源:网络整理
导读:对不起,我不清楚……让我们再试一次 我有一个记录类型: MyRecord = Record Name: string; Age: integer; Height: integer; several more fields.... 和一个INI文件: [PEOPLE]Name=MaxineAge=30maybe one or two other key/value pairs 我想要做的就是使用I
对不起,我不清楚……让我们再试一次

我有一个记录类型:

MyRecord = Record
   Name: string;
   Age: integer;
   Height: integer;
   several more fields....

和一个INI文件:

[PEOPLE]
Name=Maxine
Age=30
maybe one or two other key/value pairs

我想要做的就是使用INI文件中的数据加载记录.

我在TStringList中有来自INI的数据我希望能够遍历TStringList并仅使用TStringList中的键值对分配/更新记录字段.

查尔斯

解决方法

所以你有一个包含内容的INI文件

[PEOPLE]
Name=Maxine
Age=30

并希望将其加载到由…定义的记录中

type
  TMyRecord = record
    Name: string;
    Age: integer;
  end;

?这很容易.只需将IniFiles添加到您单位的uses子句中,然后执行

var
  MyRecord: TMyRecord;

procedure TForm1.Button1Click(Sender: TObject);
begin
  with TIniFile.Create(FileName) do
    try
      MyRecord.Name := ReadString('PEOPLE','Name','');
      MyRecord.Age := ReadInteger('PEOPLE','Age',0);
    finally
      Free;
    end;
end;

当然,MyRecord变量不必是全局变量.它也可以是局部变量或类中的字段.但这完全取决于你的确切情况.

一个简单的概括

一个稍微有趣的情况是你的INI文件包含几个人,比如

[PERSON1]
Name=Andreas
Age=23

[PERSON2]
Name=David
Age=40

[PERSON3]
Name=Marjan
Age=49

...

并且您想将其加载到TMyRecord记录数组中,然后就可以了

var
  Records: array of TMyRecord;

procedure TForm4.FormCreate(Sender: TObject);
var
  Sections: TStringList;
  i: TIniFile;
begin
  with TIniFile.Create(FileName) do
    try
      Sections := TStringList.Create;
      try
        ReadSections(Sections);
        SetLength(Records,Sections.Count);
        for i := 0 to Sections.Count - 1 do
        begin
          Records[i].Name := ReadString(Sections[i],'');
          Records[i].Age := ReadInteger(Sections[i],0);
        end;
      finally
        Sections.Free;
      end;

    finally
      Free;
    end;
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读