2013-08-26 2 views
2

아래 코드를 사용하여 텍스트 파일에서 빈 줄을 삭제하지만 작동하지 않습니다.Inno 설치 : 테스트 파일에서 빈 줄을 삭제하십시오.

function UpdatePatchLogFileEntries : Boolean; 
var 
a_strTextfile : TArrayOfString; 
iLineCounter : Integer; 
    ConfFile : String; 

begin 

ConfFile := ExpandConstant('{sd}\patch.log'); 
    LoadStringsFromFile(ConfFile, a_strTextfile); 

    for iLineCounter := 0 to GetArrayLength(a_strTextfile)-1 do 
    begin 
     if (Pos('', a_strTextfile[iLineCounter]) > 0) then 
      Delete(a_strTextfile[iLineCounter],1,1); 
    end; 
    SaveStringsToFile(ConfFile, a_strTextfile, False);    
end; 

도와주세요. 미리 감사드립니다.

답변

2

배열의 색인을 다시 생성하는 것이 비효율적이며 파일의 비어 있지 않은 행을 복사하기위한 두 개의 배열이 불필요하게 복잡하기 때문에 TStringList 클래스를 사용하는 것이 좋습니다. 그것은 당신이 필요로하는 것을 모두 안에 감쌌습니다. 코드에서 나는 다음과 같은 함수를 작성합니다.

[Code] 
procedure DeleteEmptyLines(const FileName: string); 
var 
    I: Integer; 
    Lines: TStringList; 
begin 
    // create an instance of a string list 
    Lines := TStringList.Create; 
    try 
    // load a file specified by the input parameter 
    Lines.LoadFromFile(FileName); 
    // iterate line by line from bottom to top (because of reindexing) 
    for I := Lines.Count - 1 downto 0 do 
    begin 
     // check if the currently iterated line is empty (after trimming 
     // the text) and if so, delete it 
     if Trim(Lines[I]) = '' then 
     Lines.Delete(I); 
    end; 
    // save the cleaned-up string list back to the file 
    Lines.SaveToFile(FileName); 
    finally 
    // free the string list object instance 
    Lines.Free; 
    end; 
end; 
+0

TLama @ 답장을 보내 주시면 감사하겠습니다. – user1752602

+0

당신을 진심으로 환영합니다! – TLama