Solved

text file output

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

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

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