Solved

Stringlist.SaveToFile gives extra linefeed

Posted on 2002-06-17
4
551 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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 gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
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…

758 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

22 Experts available now in Live!

Get 1:1 Help Now