1

I am using TStringList and SaveToFile. How can I tell to go a new line when string is finished? In general all strings contained in TStringList are saved in one line only. How can I tell the list to put a carriage return when finish string and need to put other string in a new line?

String is format as:

'my text....' + #10#13
Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
Marcello Impastato
  • 2,263
  • 5
  • 30
  • 52

2 Answers2

4

You could add (or insert) an empty line:

MyStringList.Add('');
MyStringList.SaveToFile(...);
Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
2

If you're writing the strings like you've shown above with 'my text....' + #10#13 + 'other text...', your problem is that you have your line ending characters reversed. On Windows, they should be #13#10 (or simply use the sLineBreak constant).

Here's a quick app (Delphi XE2) that demonstrates that the bad order of the pair will cause the problem, and a way to fix it:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes;

var
  SL: TStringList;
begin
  SL := TStringList.Create;
  try
    SL.Add('This is a test string' + #10#13 + 'This is another test string');
    SL.SaveToFile('C:\Test\BadLFPair.txt');

    SL.Clear;
    SL.Add('This is a test string'+ #13#10 + 'This is another test string');
    SL.SaveToFile('C:\Test\BadLFPairFix.txt');
  finally
    SL.Free;
  end;
end.

The first, when opened in Notepad, produces:

This is a test stringThis is another test string

The second:

This is a test string
This is another test string
Ken White
  • 123,280
  • 14
  • 225
  • 444