2009-08-07 5 views
3

ini 파일의 하나의 Ident에 배열을 쓰려면 어떻게해야합니까? 최근에 배열에서 값을 읽거나 값을 배열에 저장하려면 어떻게해야합니까? INI 파일에서 어레이를 저장하고 읽는 방법은 무엇입니까?

내가 같이하는 INI 싶습니다 방법입니다

[TestSection] 
val1 = 1,2,3,4,5,6,7 

문제 내가 가진 :

  1. 내가
  2. 크기를 사용할 수있는 기능을 모르는 정적이 아닙니다. 7 개 이상의 값일 수 있습니다. 길이가 더 짧을 수 있습니다. 어떻게 길이를 확인합니까?
  3. 당신은 이런 식으로 작업을 수행 할 수 있습니다

답변

10

,

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; 

당신은 어떤이 배열은 INI 파일에 직접 저장하는 기능을 내장이없는 등의 CSV 문자열로 정수를 저장하고로드해야합니다.

+1

입니다. –

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; 
+0

ㅎ, Re0sless는 조금 빨랐다. :) – Runner

+0

나는 너의 것도 받아 들일 수 있었으면 좋겠다. + –

+0

아무런 문제가 없기 때문에 기쁘다. :) – Runner

관련 문제