Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent TStrings.SaveToFile creating a final empty line?

I have a file .\input.txt like this:

aaa
bbb
ccc

If I read it using TStrings.LoadFromFile and write it back (even without applying any changes) using TStrings.SaveToFile, it creates an empty line at the end of the output file.

var
  Lines : TStrings;
begin
  Lines := TStringList.Create;
  try
    Lines.LoadFromFile('.\input.txt');

    //...

    Lines.SaveToFile('.\output.txt');
  finally
    Lines.Free;
  end;
end;

The same behavior can be observed using the TStrings.Text property which will return a string containing an empty line at its end.

like image 524
Fabrizio Avatar asked Oct 17 '19 07:10

Fabrizio


1 Answers

For Delphi 10.1 and newer there is a property TrailingLineBreak controlling this behavior.

When TrailingLineBreak property is True (default value) then Text property will contain line break after last line. When it is False, then Text value will not contain line break after last line. This also may be controlled by soTrailingLineBreak option.

like image 195
Uwe Raabe Avatar answered Oct 05 '22 22:10

Uwe Raabe