Solved

Stringlist.SaveToFile gives extra linefeed

Posted on 2002-06-17
4
577 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
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
SUM 2 INTEGER ARRAYS INTO 1 10 111
Wincontrol not (correctly) drawn 15 47
IP without any Dots 1 53
"Could not obtain OLE control window handle" under Delphi XE8 8 28
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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