Marcello Impastato
Marcello Impastato

Reputation: 2281

TStringList and SaveToFile: How can I tell to go a new line when string is finished?

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

Upvotes: 1

Views: 12266

Answers (2)

Ken White
Ken White

Reputation: 125757

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

Upvotes: 2

Ondrej Kelle
Ondrej Kelle

Reputation: 37221

You could add (or insert) an empty line:

MyStringList.Add('');
MyStringList.SaveToFile(...);

Upvotes: 4

Related Questions