StringList and File

Posted on 2004-11-11
Last Modified: 2010-04-05
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"
Question by:fabyola
    LVL 27

    Assisted Solution

    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 ;-)
    LVL 17

    Accepted Solution

    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.
    LVL 1

    Expert Comment

    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..


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now