从 IniFile 读取 2 行

发布于 2024-10-18 22:47:06 字数 478 浏览 3 评论 0原文

再试一次。根据建议,添加我确实理解的代码段。我对必须将 4 位信息保存在两行中这一事实感到满意,如下所示:

IniFile.WriteString('TestSection','Name','Country');
IniFile.WriteString('TestSection','City','Street');

我的问题更多是有关将此信息加载回表单中。例如,如果在我的 IniFile 中我保存了以下代码,则

[TestSection]
John=Uk
London=barlystreet
Mike=Spain
Madrid=eduardostrata
Emma=USA
New York=1st Avenue

在 IniFile 中组成了信息。通过上面的代码添加。 现在我的问题是:例如,当我在编辑框中键入 Mike 时,我如何加载其余的所属信息。(西班牙,马德里,eduardostrata)。

Trying again. On advice, adding the piece of code that I do understand. I am fine with the fact that I have to save 4 bits of information in two lines like so:

IniFile.WriteString('TestSection','Name','Country');
IniFile.WriteString('TestSection','City','Street');

My question is more about loading this information back into the form. If in my IniFile I have saved for example the following code

[TestSection]
John=Uk
London=barlystreet
Mike=Spain
Madrid=eduardostrata
Emma=USA
New York=1st Avenue

Made up information in the IniFile. Added through the code above.
Now my question is: How could I load for example, when I type in an edit box Mike, the rest of the belonging information.(Spain, Madrid,eduardostrata).

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

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

发布评论

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

评论(3

会发光的星星闪亮亮i 2024-10-25 22:47:06

INI 文件不是这样工作的。您保存name=value对,并且必须有一种方法将它们关联起来。

也许这可以帮助您开始:

Ini := TIniFile.Create(YourIniFileName);
try
  Ini.WriteString('Mike', 'Country', 'Spain');
  Ini.WriteString('Mike', 'City', 'Madrid');
  Ini.WriteString('Mike', 'Street', 'EduardoStrata');
finally
  Ini.Free;
end;

您的 INI 文件中的结果包含:

[Mike]
Country=Spain
City=Madrid
Street=EduardoStrata

加载回来:

var
  Country, City, Street: string;
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(YourIniFilename);
  try
    Country := Ini.ReadString('Mike', 'Country', '<None>');
    City := Ini.ReadString('Mike', 'City', '<None>');
    Street := Ini.ReadString('Mike', 'Street', '<None>');
  finally
    Ini.Free;
  end;
  // Country, City, and Street now equal the values for 'Mike',
  // or they contain '<None>' if the section 'Mike' doesn't
  // exist or has no values for the variable.
end;

所以您可能可以弄清楚这是如何工作的。该部分([] 中的部分)是人名,名称/值对是位置及其相应的值(例如,“Country=Spain”)。

That's not how an INI file works. You save name=value pairs, and have to have a way to associate them.

Maybe this can help you get started:

Ini := TIniFile.Create(YourIniFileName);
try
  Ini.WriteString('Mike', 'Country', 'Spain');
  Ini.WriteString('Mike', 'City', 'Madrid');
  Ini.WriteString('Mike', 'Street', 'EduardoStrata');
finally
  Ini.Free;
end;

Results in your INI file containing:

[Mike]
Country=Spain
City=Madrid
Street=EduardoStrata

To load back:

var
  Country, City, Street: string;
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(YourIniFilename);
  try
    Country := Ini.ReadString('Mike', 'Country', '<None>');
    City := Ini.ReadString('Mike', 'City', '<None>');
    Street := Ini.ReadString('Mike', 'Street', '<None>');
  finally
    Ini.Free;
  end;
  // Country, City, and Street now equal the values for 'Mike',
  // or they contain '<None>' if the section 'Mike' doesn't
  // exist or has no values for the variable.
end;

So you can probably figure out how this works. The section (the part in []) is the person's name, and the name/value pairs are the location and it's corresponding value (for instance, 'Country=Spain').

等数载,海棠开 2024-10-25 22:47:06

您显然不了解 INI 文件的工作原理。如果约翰和戴夫都住在纽约怎么办? INI 文件中不能有两个同名的键。 (此外,您不应依赖每个部分中行的顺序。)

因此,您需要重新考虑如何保存数据。一个非常简单的解决方案是使用纯文本文件,其中每一行都是数据库中的一个项目,并且字段之间用竖线 (|) 等分隔:

John|Uk|London|barlystreet
Mike|Spain|Madrid|eduardostrata
Emma|USA|New York|1st Avenue.

如何读取此文件?嗯,这很简单,您应该知道如何做到这一点。如果您有非常具体的问题,请随时提问。

INI 文件的用途

那么,INI 文件的用途是什么? INI 文件的典型应用是保存程序设置。例如,当您退出文本编辑器时,它可能会将设置保存到settings.ini:

[Font]
Name=Consolas
Size=10

[Behaviour]
AutoIndent=1
AutoReplace=1
AutoBrackets=1
BracketHighlight=1
SyntaxHighlight=1

[Window]
Width=800
Height=600
Maximized=0

etc。这是由

WriteString('Font', 'Name', Editor.Font.Name);
WriteInteger('Font', 'Size', Editor.Font.Size);

etc完成的。当您下次启动应用程序时,它会读取该文件以恢复设置:

Editor.Font.Name := ReadString('Font', 'Name', 'Consolas');
Editor.Font.Size := ReadInteger('Font', 'Size', 10);

etc。 ,其中最后一个参数是默认值(以防 INI 文件中缺少该字段)。请注意,在每个部分中,键都是唯一的(并且需要如此),并且我们不关心每个部分内键的相对顺序。

You have clearly not understood how INI files work. What if both John and Dave live in New York? You cannot have two keys with the same name in an INI file. (In addition, you shouldn't rely on the ordering of the lines within each section.)

You thus need to rethink how you save your data. A very simple solution is to use a plain text file in which each line is an item in your database, and the fields are seperated by, for instance, a vertical line (|):

John|Uk|London|barlystreet
Mike|Spain|Madrid|eduardostrata
Emma|USA|New York|1st Avenue.

How to read this file? Well, that is trivial, and you should know how to do that. If you have a very specific question, then feel free to ask.

What INI files are for

But what are INI files for, then? Well, a typical application of a INI file is to save program settings. For instance, when you quit a text editor, it might save the settings to settings.ini:

[Font]
Name=Consolas
Size=10

[Behaviour]
AutoIndent=1
AutoReplace=1
AutoBrackets=1
BracketHighlight=1
SyntaxHighlight=1

[Window]
Width=800
Height=600
Maximized=0

etc. This is done by

WriteString('Font', 'Name', Editor.Font.Name);
WriteInteger('Font', 'Size', Editor.Font.Size);

etc. And when you start the application the next time, it will read the file to restore the settings:

Editor.Font.Name := ReadString('Font', 'Name', 'Consolas');
Editor.Font.Size := ReadInteger('Font', 'Size', 10);

etc., where the last parameters are the default values (in case the field is missing in the INI file). Notice that in each section, the keys are unique (and need to be), and that we don't care about the relative order of the keys inside each section.

乞讨 2024-10-25 22:47:06

创建所需关系的最简单方法是将用户的所有详细信息放在一行文本中。这不是 INI 文件的工作。你的问题是如何解析字符串。

首先,为什么需要保存“重复密码”?这对我来说没有意义。通常,用户界面会要求用户重复密码作为一种验证形式,但这就是它的全部好处。存储它以供以后检索没有任何好处。

我认为您需要保存用户的名字、姓氏和密码(3 个字符串)。看看下面的代码。

procedure SaveUserDetails(sFileName: string);
var
  sFirstName, sLastName, sPassword: string;
  slUsers: TStringList;
begin
  sFirstName := txtFirstName.Text;  // these could be from TEdit controls for example
  sLastName := txtLastName.Text;
  sPassword := txtPassword.Text;
  slUsers := TStringList.Create;
  slUsers.Add(sFirstName + ',' + sLastName + ',' + sPassword);
  slUsers.SaveToFile(sFileName);  // that has saved your stringlist to a file
  slUsers.Free;
end;

该文件将如下所示

Shane,Warne,cricket

现在,如何加载它...

procedure LoadUserDetails(sFileName: string);
var
  sFirstName, sLastName, sPassword: string;
  sTemp: string;
  slUsers: TStringList;
  iPos: integer;  // string position marker we'll use to split the string in 3
begin
  slUsers := TStringList.Create;
  slUsers.LoadFromFile(sFileName); // this loads the file's contents into stringlist
  sTemp := slUsers[0];
  if (Length(sTemp) > 0) then // just to check that there is data there
  begin
    iPos := pos(',', sTemp); // get position of first comma (our "delimiter")
    sFirstName := Copy(sTemp, 0, iPos-1);  // firstName everything upto 1st comma
    sTemp := Copy(sTemp, iPos + 1, Length(sTemp)); // chop off bit we just read
    iPos := pos(',', sTemp); // get position of second comma
    sLastName := Copy(sTemp, 0, iPos-1);  // LastName everything upto 2nd comma
    sTemp := Copy(sTemp, iPos + 1, Length(sTemp)); // chop off bit we just read
    sPassword := sTemp; // that's it
  end;
  slUsers.Free;
end;

现在...距离“好代码”还很远,但现在您至少知道一种方法来完成您的工作。希望有帮助。

The easiest way to create the relationship you want is to have all of a user's details on 1 line of text. This is not a job for INI files. Your issue is how to parse strings.

First of all, why do you need to save the "repeat password"? that doesn't make sense to me. Usually a UI will ask the user to repeat the password as a form of validation, but that's all it's good for. There's no benefit in storing it for later retrieval is there.

I think you need to save the user's first_name, last_name, and password (3 strings). Have a look at the following piece of code.

procedure SaveUserDetails(sFileName: string);
var
  sFirstName, sLastName, sPassword: string;
  slUsers: TStringList;
begin
  sFirstName := txtFirstName.Text;  // these could be from TEdit controls for example
  sLastName := txtLastName.Text;
  sPassword := txtPassword.Text;
  slUsers := TStringList.Create;
  slUsers.Add(sFirstName + ',' + sLastName + ',' + sPassword);
  slUsers.SaveToFile(sFileName);  // that has saved your stringlist to a file
  slUsers.Free;
end;

The file will look this

Shane,Warne,cricket

Now, how to load it...

procedure LoadUserDetails(sFileName: string);
var
  sFirstName, sLastName, sPassword: string;
  sTemp: string;
  slUsers: TStringList;
  iPos: integer;  // string position marker we'll use to split the string in 3
begin
  slUsers := TStringList.Create;
  slUsers.LoadFromFile(sFileName); // this loads the file's contents into stringlist
  sTemp := slUsers[0];
  if (Length(sTemp) > 0) then // just to check that there is data there
  begin
    iPos := pos(',', sTemp); // get position of first comma (our "delimiter")
    sFirstName := Copy(sTemp, 0, iPos-1);  // firstName everything upto 1st comma
    sTemp := Copy(sTemp, iPos + 1, Length(sTemp)); // chop off bit we just read
    iPos := pos(',', sTemp); // get position of second comma
    sLastName := Copy(sTemp, 0, iPos-1);  // LastName everything upto 2nd comma
    sTemp := Copy(sTemp, iPos + 1, Length(sTemp)); // chop off bit we just read
    sPassword := sTemp; // that's it
  end;
  slUsers.Free;
end;

Now... this far from "good code" but now you know at least 1 way to do your thing. Hope that helps.

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