Board index » delphi » TStringlist loadfromfile adds blank lines?

TStringlist loadfromfile adds blank lines?

I created a TStringlist and used loadfromfile to load in a text
document.  It works fine, except when I look at the results in a memo
for example, i noticed that where it found blank lines it added extra
blank lines.  Now i have a ton of white space.  How can I prevent it
from making these double blank lines, or how can I get rid of them.  I
only want to reduce the double blanks, not all blank
lines

Any ideas?

 

Re:TStringlist loadfromfile adds blank lines?


Quote
"scrptman" <scrpt...@aol.com> wrote in message

news:f08beb45.0210250528.64df7c85@posting.google.com...

Quote
> I created a TStringlist and used loadfromfile to load in a text
> document.  It works fine, except when I look at the results in a memo
> for example, i noticed that where it found blank lines it added extra
> blank lines.  Now i have a ton of white space.  How can I prevent it
> from making these double blank lines, or how can I get rid of them.  I
> only want to reduce the double blanks, not all blank
> lines

Its likely that the text file actually contains multiple CR and/or LF
characters. I've not seen a string list actually add any lines on a whim. If
the source file can't be easily corrected, try something like

procedure stripExtraneousBlankLines (aSL : tStringList);

var i : integer;

begin
for i := (aSL.Count - 1) downto 1 do
    begin
    if (Trim (aSL [i]) = '') and (Trim (aSL [i - 1]) = '')
    then aSL.Delete (i);
    end;
end;

Re:TStringlist loadfromfile adds blank lines?


In article <f08beb45.0210250528.64df7...@posting.google.com>, Scrptman
wrote:

Quote

> I created a TStringlist and used loadfromfile to load in a text
> document.  It works fine, except when I look at the results in a memo
> for example, i noticed that where it found blank lines it added extra
> blank lines.  Now i have a ton of white space.  How can I prevent it
> from making these double blank lines, or how can I get rid of them.  I
> only want to reduce the double blanks, not all blank
> lines

Try looking at the offending file with a hex editor to see if the
newline characters are OK (it should be "0D0A" and *not* e.g "0D0D0A").

Regards, Ren

Re:TStringlist loadfromfile adds blank lines?


On 25 Oct 2002 06:28:38 -0700, Scrptman wrote:

Quote
> I created a TStringlist and used loadfromfile to load in a text
> document.  It works fine, except when I look at the results in a memo
> for example, i noticed that where it found blank lines it added extra
> blank lines.  Now i have a ton of white space.  How can I prevent it
> from making these double blank lines, or how can I get rid of them.  I
> only want to reduce the double blanks, not all blank
> lines

if you load into a stringlist (untested):

var
  num, posnum : integer;
begin
   for Stringlist.Count -1 downto 0 do
   begin
     posnum := pos(str,'');
     if posnum <> 0 then Stringlist.Delete(num);
   end;  

you should be able use the above loading directly into a Memo
(untested):

var
  num, posnum : integer;
begin
  Memo1.BeginUpdate;
  Memo1.Lines.LoadfromFile(aFile);
  for Memo1.Lines.Count -1 downto 0 do
   begin
     posnum := pos(str,'');
     if posnum <> 0 then Memo1.Lines.Delete(num);
   end;
   Memo1.EndUpdate;
====
Brad Blanchard

Other Threads