Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TStringList.LoadFromFile - double carriage returns

Status
Not open for further replies.

MichaelHooker

Programmer
Mar 17, 2006
70
GB
I use Delphi 7 in Win XP SP2.

One of my programs loads a text file into a StringList for processing. After 7 months of daily files, the external provider has somehow started producing files where each line ends with not one but two carriage returns and a linefeed. When viewed in NotePad the files look perfectly normal, but a hex editor clearly shows "0D 0D 0A" at the end of every line rather than the previous "0D 0A".

When the file is loaded into a StringList using .LoadFromFile(), the resulting StringList consists of alternating lines of data and blank lines and as you can imagine the unexpected blank lines play havoc with my processing.

The same behaviour occurs with, eg:

Code:
Memo1.Lines.LoadFromFile(FileName);

the result being that data lines alternate with blank lines in the Memo.

The fact that two lines are being created from one is also illustrated by the fact that if I use .SaveToFile() from the StringList or Memo, and then load the resulting file into NotePad, I get alternate blank lines: in the hex viewer there is "0D 0A 0D 0A" between each row of text!

I could I suppose include a new line in the processing loop which says:

Code:
  if LineString <> '' then begin
     etc;
     etc;
  end;

But there should be a more elegant way, shouldn't there? And if NotePad can successfully ignore the extra CR character why can't Delphi components?

I have no control over the format of the input file or how it is produced: so the answer to this problem cannot be "get the input file format changed back to what it was like before"!

I would provide an example program to play with but I can't work out how to reproduce a sample of the source file, short of an attachment to this post and it doesn't seem possible to do that.

Thanks in any advance for any helpful comments and suggestions.

Michael Hooker
 
preprocess your stringlist.

stringlist converts lonely carriage returns into CR/LF
so only one line is needed

memo1.Text := stringReplace(Memo1.text, #13#10#13#10, #13#10, [rfReplaceAll]);

to be clear:
#13 = CR = 0D hex
#10 = LF = 0A hex

stringreplace function can be found in SysUtils unit

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
memo1.Text := stringReplace(Memo1.text, #13#10#13#10, #13#10, [rfReplaceAll]);

Thanks. Or in my case, since I'm actually working with the StringList, and the Memo example was only by way of further illustration, presumably this will work:

Code:
MyList.Text := stringReplace(MyList.text, #13#13#10, #13#10, [rfReplaceAll]);

I was hoping for some obscure setting that would actually change the behaviour of .LoadFromFile rather than preprocessing anything. But never mind.
 
PS :
be aware that the stringlist converts the standalone CR to
CRLF so you must use #13#10#13#10 in the function.

if you really don't like the current TStringList behaviour
you jave 2 alternatives :
- make your own TStringList.
-Im sure there are some freeware alternatives out there, so google TstringList Delphi.
Happy searching!

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top