2013-08-26 22 views
2

我们使用下面的代码从文本文件中删除空行,但它不起作用。Inno Setup:从测试文件中删除空行

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