Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

text file output

Posted on 2002-05-10
6
Medium Priority
?
171 Views
Last Modified: 2010-04-04
I want to create a procedure to write a TStringList in a text file. Procedure prototype should be like this:
procedure Log(text: TStringList, filename: ?);

The procedure must always append at the end if filename exists or create the file if it doesn't exist.

This procedure must do the entire process from opening and writting to closing the file (as simple as possible).
0
Comment
Question by:Kurtis
[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
6 Comments
 
LVL 8

Accepted Solution

by:
Cesario earned 400 total points
ID: 7000965
Dear Kurtis,

try this simple demo:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    LogFile : TextFile;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure Log(text: TStringList; filename:String);
var
  txt : Textfile;
Begin
  AssignFile ( Txt,Filename);

  IF FileExists ( Filename )
    Then Append (txt)
    Else Rewrite (txt);

  Write ( txt,Text.Text);

  closefile(txt);

End;

procedure TForm1.Button1Click(Sender: TObject);
vAR
  logs : tStringlist;
  cnt  : Integer;
begin

  Logs := tStringlist.Create;

  For cnt := 1 to 100 do
    logs.Add ( intTostr(cnt));
  log ( logs,'C:\log.txt');

  logs.Clear;
  For cnt := 200 to 300 do
    logs.Add ( intTostr(cnt));
  log ( logs,'C:\log.txt');


end;

end.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 7000968
Hi Kurtis,

something like this:

procedure AddToFile(L: TStrings; const FName: String);
var
  LL: TStringList;
begin
  LL := TStringList.Create;
  if FileExists(FName) then
    LL.LoadFromFile(FName);
  LL.Text := LL.Text + L.Text;
  LL.SaveToFile(FName);
  LL.Free;
end;
   
----
Igor.
0
 
LVL 8

Expert Comment

by:Cesario
ID: 7000976
Hi ITugay,

Cool answer ;-)
But you can use it with small log files. If the Size of the Logfile let say 100 MB. So you have to read all the file and add the new liens and add it again.

Best Regards

Cesario
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:ITugay
ID: 7000981

just to demonstrate another way then you use :-)
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7001105
procedure Log(List: TStringList; FileName: string);
var
  Stream: TFileStream;
begin
  if FileExists(FileName) then
    Stream := TFileStream.Create(FileName, fmOpenReadWrite)
  else
    Stream := TFileStream.Create(FileName, fmCreate);
  Stream.Seek(0, soFromEnd);
  List.SaveToStream(Stream);
  Stream.Free;
end;
0
 

Author Comment

by:Kurtis
ID: 7001622
That is how a question is answered, thank you very much!
0

Featured Post

Technology Partners: 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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

604 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