开发者

reading 2 lines from IniFile

开发者 https://www.devze.com 2023-02-13 14:32 出处:网络
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:

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.开发者_JS百科(Spain, Madrid,eduardostrata).


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号