?
Solved

text file output

Posted on 2002-05-10
6
Medium Priority
?
169 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

801 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