Solved

text file output

Posted on 2002-05-10
6
165 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 100 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
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!

 
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

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

Suggested Solutions

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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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