StringList and File

I´m creating a program in which it has to write everything that has in a StringList. I declared a variable "List: TStrings;"  then I create my String List using "List := TStringList.Create;". Than I put everything in it. It can come up to 100 or more lines. Than I have to send everything to a text file. My problem is that everytime I write my List from my StringList to my text file, it comes up with a blank line. Example:

1. Line -> Apple
2. Line -> Orange
3. Line -> Banana
4 .Line ->

So the line 4 comes up with nothing (Blank). It´s like just doing an "Enter" on the end of the stringlist and typing nothing. I need to take that enter off before writing to my file. Because the file also is comingo up with an "Enter". How do I do that ? I put a memo to my application and sent everything on my StringList to it to see if the enter is on my StringList and on the memo is also coming up with the "Enter"
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

usual there is allways a crlf after each line if u save a tstringlist,
so also on the last entry

you may write your file with your own routine like

  s : string;
  f : text;
  s := myList.Text;
  delete(s,length(s)-2,2);  //delete last two chars (crlf)

just from head

meikl ;-)
Wim ten BrinkSelf-employed developerCommented:
Just use:

  AssignFile( Output, 'Filename.txt' ) );
  Rewrite( Output );
  if ( List.Count > 0 ) then begin
    for I := 0 to Pred( Pred( List.Count ) ) do begin
      WriteLn( Output, List[ I ] );
    Write( Output, List[ Pred( List.Count ) ] );
  CloseFile( Output );

Or use:

  AssignFile( Output, ChangeFileExt( ParamStr( 0 ), '1.txt' ) );
  Rewrite( Output );
  Write( Output, Copy( List.Text, 1, Length( List.Text ) - 2 ) );
  CloseFile( Output );

The additional linebreak is just typical for stringlists. Even worse, the additional linebreak is ADDED to the Text property when you call it, but not when you assign a value to the Text property. When you walk through the list to write line by line, all you have to remember is to not use WriteLn for the last line. And if you just want to write the Text property, skip the last two characters.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Won't it be simpler to use the memo's lines TStrings and simply do:
Memo.Lines.SaveToFile(FileName) ? :)

Just a thought...

if that is not the case and you must use the List (TStringList) you can always
assign the memo's lines to it:

and becuase you have a CRLF as the last line you can always do this:

for i := 0 to List.Count - 1 do
  if List.Strings[i] <> '' then
    //write to file here..

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.