Mawg
Mawg

Reputation: 40205

Processing both Unix & DOS files

My app proccess text files which are somtimes produced on WIndwos systems & sometimes Linux.

What is the minimal effort way to handle both line endings?

That is to say, for each line, I want to get a string with the line ending stripped.

Upvotes: 2

Views: 687

Answers (1)

Ken White
Ken White

Reputation: 125757

TStringList handles both Windows and Linux line endings just fine.

program TestLFs;

{$APPTYPE CONSOLE}
uses
  Classes;

var
  SL: TStringList;
  s: string;
begin
  SL := TStringList.Create;
  try
    SL.LoadFromFile('YourUnixFile.txt');
    for s in SL do
      WriteLn(s);
    SL.LoadFromFile('YourWindowsFile.txt');
    for s in SL do
      WriteLn(s);
  finally
    SL.Free;
  end;
  ReadLn;
end.

Upvotes: 4

Related Questions