Reading 2 lines from IniFile

Repeated repetition. On the advice of adding a piece of code that I really understand. I am fine with the fact that I need to save 4 bits of information in two lines:

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

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

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

Created information in IniFile. Added through the code above. Now my question is: how can I load, for example, when I enter Mike in the edit box, the rest of the information that belongs. (Spain, Madrid, eduardostrata).

+3
source share
3 answers

This is not how the INI file works. You save pairs name=valueand should have a way to link them.

Perhaps this will 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 download 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;

, , , , . ( []) - , / - (, " = " ).

+13

, 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

...

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

... , :

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

.., ( INI ). , ( ), .

+3

, , , . INI. , .

First of all, why do you need to save a "retry password"? it doesn't make sense to me. Typically, the user interface asks the user to repeat the password as a form of verification, but all this is good. There is no gain in storage for later retrieval.

I think you need to save username first_name, last_name and password (3 lines). Take a look at the following code snippet.

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 like this:

Shane,Warne,cricket

Now how to download 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 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 it
  end;
  slUsers.Free;
end;

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

+2
source

Source: https://habr.com/ru/post/1794272/


All Articles