?
Solved

Stringlist.SaveToFile gives extra linefeed

Posted on 2002-06-17
4
Medium Priority
?
610 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 400 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 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