Solved

Stringlist.SaveToFile gives extra linefeed

Posted on 2002-06-17
4
585 Views
Last Modified: 2010-04-04
This question is in two parts
ONE: If I have a text file that ends in a carraige return, linefeed pair (CRLF) how do I change the file to get rid of the final CRLF?
Example:
from
    line1CRLF
    line2CRLF

to
    line1CRLF
    line2


TWO:Why does a TStringList add a CRLF to its end where TStrings doesn't seem to?

This bit of code adds an extra CRLF to the saved file
procedure TForm1.Button5Click(Sender: TObject);
var
    i : integer;
    temp : string;
    slist : TStringList;
begin
    temp := '';
    for i := 1 to 3 do
    begin
        temp := temp + #13#10 + intToStr(i);
    end;
    temp := 'records = ' + intToStr(i) + temp;
    slist := TStringList.create;
    slist.Text := temp;
    memo1.Lines := sList;
    Memo1.Lines.SaveToFile('test1.txt');
    sList.Free;
end;


This bit of code does not add an extra CRLF???
procedure TForm1.Button4Click(Sender: TObject);
var
    i : integer;
    temp : string;
begin
    temp := '';
    for i := 1 to 3 do
    begin
        temp := temp + #13#10 + intToStr(i);
    end;
    temp := 'records = ' + intToStr(i) + temp;
    memo1.Lines.Text := temp;
    Memo1.Lines.SaveToFile('test1.txt');
end;

0
Comment
Question by:seabear
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:seabear
ID: 7085025
Things have just got worse. If I open the test files created in either of the code fragments above using the DOS Edit program I see a blank line at the end of the file. However if I open the files using Notepad I only see a blank line on the file created by the first code example.

I am trying to create data files to import into a program that will not accept any file with a blank line at the end. So what I need to do is write code that saves data to a text file and shows no blank lines at the end of the file even when viewed with Edit.
0
 

Accepted Solution

by:
lottol earned 100 total points
ID: 7085915
TStrings is abstract class.
TMemo.Lines is TMemoStrings indeed.
TMemoStrings doesn't need a CRLF at the end instead of TStringList.

If you open result of Button4Click in a HEX view, I'll see that it hasn't blank line at the end.


You can also try this methode.

procedure TForm1.Button1Click(Sender: TObject);
var
   i : integer;
   temp : string;
   FileStream: TFileStream;
begin
   temp := '';
   for i := 1 to 3 do
       temp := temp + #13#10 + intToStr(i);
   temp := 'records = ' + intToStr(i) + temp;

   FileStream := TFileStream.Create('test1.txt', fmCreate);
   try
    FileStream.Write(Temp[1], length(Temp));
   finally
    FileStream.Free;
   end;
end;
0
 
LVL 4

Expert Comment

by:nestorua
ID: 7101544
HI,
You can save your StringList into the FileStream and then
decrease it Size on 2.
Sincerely,
Nestorua.
0
 

Author Comment

by:seabear
ID: 7121023
Thanks lottol I now have it all working - Dan
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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