Delphi 如何从INI文件中存储和读取数组?
如何将一个数组写入ini文件中的一个标识,最近如何从中读取并将值存储在数组中 这是我希望ini的外观:Delphi 如何从INI文件中存储和读取数组?,delphi,Delphi,如何将一个数组写入ini文件中的一个标识,最近如何从中读取并将值存储在数组中 这是我希望ini的外观: [TestSection] val1 = 1,2,3,4,5,6,7 我的问题是: 我不知道我必须使用的功能 大小不是静态的。它可能大于7个值,也可能小于7个值。如何检查长度 你可以这样做 uses inifiles procedure ReadINIfile var IniFile : TIniFile; MyList:TStringList; begin MyList
[TestSection]
val1 = 1,2,3,4,5,6,7
我的问题是:
你可以这样做
uses inifiles
procedure ReadINIfile
var
IniFile : TIniFile;
MyList:TStringList;
begin
MyList := TStringList.Create();
try
MyList.Add(IntToStr(1));
MyList.Add(IntToStr(2));
MyList.Add(IntToStr(3));
IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
try
//write to the file
IniFile.WriteString('TestSection','Val1',MyList.commaText);
//read from the file
MyList.commaText := IniFile.ReadString('TestSection','Val1','');
//show results
showMessage('Found ' + intToStr(MyList.count) + ' items '
+ MyList.commaText);
finally
IniFile.Free;
end;
finally
FreeAndNil(MyList);
end;
end;
您必须将整数保存并加载为CSV字符串,因为没有内置函数将数组直接保存到ini文件。您不需要长度说明符。分隔符清楚地分隔了数组的各个部分 如果INI文件中有这样定义的节
[TestSection]
val1 = 1,2,3,4,5,6,7
那么你所要做的就是
procedure TForm1.ReadFromIniFile;
var
I: Integer;
SL: TStringList;
begin
SL := TStringList.Create;
try
SL.StrictDelimiter := True;
SL.CommaText := FINiFile.ReadString('TestSection', 'Val1', '');
SetLength(MyArray, SL.Count);
for I := 0 to SL.Count - 1 do
MyArray[I] := StrToInt(Trim(SL[I]))
finally
SL.Free;
end;
end;
procedure TForm1.WriteToIniFile;
var
I: Integer;
SL: TStringList;
begin
SL := TStringList.Create;
try
SL.StrictDelimiter := True;
for I := 0 to Length(MyArray) - 1 do
SL.Add(IntToStr(MyArray[I]));
FINiFile.WriteString('TestSection', 'Val1', SL.CommaText);
finally
SL.Free;
end;
end;
嘿,Re0sless快了一点:)仅供参考,数组中的游荡空格将通过它关闭。@JimMcKeeth您只需设置StrictDelimiter:=true。那就不是问题了。