Solved

text file output

Posted on 2002-05-10
6
160 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
6 Comments
 
LVL 8

Accepted Solution

by:
Cesario earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 9

Expert Comment

by:ITugay
Comment Utility

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

Expert Comment

by:robert_marquardt
Comment Utility
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
Comment Utility
That is how a question is answered, thank you very much!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

16 Experts available now in Live!

Get 1:1 Help Now