[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Stringlist.SaveToFile gives extra linefeed

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

Industry Leaders: 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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

872 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