Re: Stopping Delphi ReadLn from removing lone CR's

Posted by Jenny, DelphiLand Team on August 19, 2008

In Reply to Stopping ReadLn from removing lone CR's posted by Lee P12462 on August 16, 2008

: I have a file with records, delimited by the usual CR/LF. In some records there are also lone CR's separating the various pieces of a postal address. ReadLn is removing those lone CR's!
: Is there a flag or something that can be set to change this behavior of ReadLn?

As far as I know, there is no flag in Delphi for ReadLn to change this behavior.

ReadLn interprets the CR as an end-of-line, just as it does with the CR/LF combination. Let's suppose that you wrote a line with the code below, where you embedded a CR (code #13) in a line:

WriteLn(TheFile, 'Part 1' + #13 + 'Part 2'); // writes line and appends CR and LF

When you try to read that line with:

ReadLn(TheFile, S);

then you only get 'Part 1'. The next ReadLn will give you 'Part 2'. Object Pascal sees this as two lines!

Tip: in writing text files, better use some other character as a field separator, such as the widely used TAB.
But if your text files already exist, I suggest obtaining or writing a routine that replaces the "lone" CR characters with TAB characters.

Related articles & Follow Ups