I want to know how I can write an Inifile so that I can read from it in order.
First of all writing it: Let say I have 2 strings I want to save (Name and Lastname).
This is a button. So it will be clicked everytime:
Ini.WriteString(person1, 'Name', Name.text);
Ini.WriteString(person1, 'Lastname', Lastname.text);
How can I make this person1, alter everytime a new person is added. so next time person开发者_高级运维2, person3, etc. How is it possible? Do I have to look at what the previous one said? or can it remember? Maybe if I understand this, reading from the file will go in same maner.
kind regards and thank you
I suppose you want to store n data items in an INI file. I do that all the time -- it's easy. First, saving is trivial, but of course, the exact procedure depends on where you get the data from. If you have two arrays of strings, for example (let's call them FirstNames
and LastNames
), then you just do
for i := 0 to high(FirstNames) do
begin
IniFile.WriteString('Names', 'FirstName' + IntToStr(i), FirstNames[i]);
IniFile.WriteString('Names', 'LastName' + IntToStr(i), LastNames[i]);
end;
To read the (unknown number of items), do something like
for i := 0 to MaxInt do
if ValueExists('Names', 'FirstName' + IntToStr(i)) then
// Do something with ReadString('Names', 'FirstName' + IntToStr(i))
// and ReadString('Names', 'LastName' + IntToStr(i))
else
break;
Notice that 'Names'
is the arbitrary name of the section in the INI file under which you store the data.
Update
If you just want to save items when clicking a button, why not do
private
{ Private declarations }
n: integer;
and
procedure TForm1.FormCreate(Sender: TObject);
begin
n := 0;
end;
and then
procedure TForm1.Button1Click(Sender: TObject);
begin
with TIniFile.Create('myfile.ini') do
try
WriteString('Names', 'FirstName' + IntToStr(n), Edit1.Text);
WriteString('Names', 'LastName' + IntToStr(n), Edit2.Text);
inc(n);
finally
Free;
end;
end;
If you instead would prefer to have a section per item, do
procedure TForm1.Button1Click(Sender: TObject);
begin
with TIniFile.Create('myfile.ini') do
try
WriteString('Name' + IntToStr(n), 'FirstName', Edit1.Text);
WriteString('Name' + IntToStr(n), 'LastName', Edit2.Text);
inc(n);
finally
Free;
end;
end;
instead.
To get sequential numbering of sections each time you save a person you would have to read all the section names, determine the highest number, increment that and then use it to write the a new section with that name and the new person's values.
Something like:
var
IniFile: TIniFile;
SL: TStringList;
i: Integer;
Highest: Integer;
begin
IniFile := TIniFile.Create('MyIni.ini');
try
SL := TStringList.Create;
try
IniFile.ReadSections(SL);
Highest := 0;
for i := 0 to SL.Count - 1 do begin
Highest := Max(Highest, StrToIntDef(Copy(SL[i], Length('Person'), MAXINT), 0));
end;
IniFile.WriteString('Person' + IntToStr(Highest), 'Name', Name.Text);
IniFile.WriteString('Person' + IntToStr(Highest), 'LastName', LastName.Text);
finally
SL.Free;
end;
finally
IniFile.Free;
end;
end;
I suspect that using an ini file to do this is the wrong way to go about it. Ini files are not designed to store things in order. The simplest way of achievng this is to use a TStringList and save and load it to disk.
精彩评论