Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading 2 lines from IniFile

Tags:

ini

delphi

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).

like image 239
Lakkerw Avatar asked Dec 01 '22 08:12

Lakkerw


1 Answers

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').

like image 115
Ken White Avatar answered Dec 09 '22 15:12

Ken White