如何在ini文件中将一个数组写入一个Ident,最近如何从中读取并将值存储在数组中?
这就是我喜欢ini的样子:
[TestSection]
val1 = 1,2,3,4,5,6,7
我遇到的问题:
答案 0 :(得分:12)
您不需要长度说明符。分隔符清楚地界定了数组的各个部分。
如果你在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;
答案 1 :(得分:11)
你可以这样做,
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文件中。