我们正在使用下面的代码从文本文件中删除空行,但是它不起作用。
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;
请帮我。提前致谢。
因为数组的重新索引效率低下,并且拥有两个用于复制文件非空行的数组会不必要地变得复杂,所以建议您使用TStringList
class。它囊括了您需要的所有内容。在代码中,我将这样编写函数:
[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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句