如何在delphi 7中将INI节分配给记录

发布于 2024-11-17 01:59:48 字数 412 浏览 0 评论 0原文

抱歉,我不清楚...让我们再试一次

我有一个记录类型:

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 中具有键值对的记录字段。

查尔斯

I'm sorry I'm not being clear...lets try again

I have a record type :

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

and an INI file with:

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

All I want to do is load the record with the data from the INI file.

I have the data from the INI in a TStringList I want to be able to loop through the TStringList and assign/update only the Record Fields with key value pairs in the TStringList.

Charles

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛徒 2024-11-24 01:59:48

因此,您有一个包含内容的 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], 'Name', '');
          Records[i].Age := ReadInteger(Sections[i], 'Age', 0);
        end;
      finally
        Sections.Free;
      end;

    finally
      Free;
    end;
end;

So you have an INI file with the content

[PEOPLE]
Name=Maxine
Age=30

and want to load it into a record defined by

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

? That is very easy. Just add IniFiles to the uses clause of your unit, and then do

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;

Of course, the MyRecord variable need not be a global variable. It can also be a local variable or a field in a class. But that all depends on your exact situation, naturally.

A Simple Generalisation

A slightly more interesting situation is if your INI files contains several people, like

[PERSON1]
Name=Andreas
Age=23

[PERSON2]
Name=David
Age=40

[PERSON3]
Name=Marjan
Age=49

...

and you want to load it into an array of TMyRecord records, then you can do

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], 'Name', '');
          Records[i].Age := ReadInteger(Sections[i], 'Age', 0);
        end;
      finally
        Sections.Free;
      end;

    finally
      Free;
    end;
end;
江南烟雨〆相思醉 2024-11-24 01:59:48

如果字符串列表中有 INI 部分,则只需使用 Values[] 属性:

字符串列表内容

Name=Maxine
Age=30

读取记录的代码

MyRecord.Name := StringList.Values['Name']
MyRecord.Age = StrToInt(StringList.Values['Age'])

您可能希望以一种或另一种方式处理错误,但这是基本思想。

If you have the INI section in a string list you can just use the Values[] property:

String list contents

Name=Maxine
Age=30

Code to read into record

MyRecord.Name := StringList.Values['Name']
MyRecord.Age = StrToInt(StringList.Values['Age'])

Naturally you would want to handle errors one way or another, but this the the basic idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文