Board index » delphi » Text output to a log file?

Text output to a log file?

Newbie here. Sorry if this has already been covered.  I have spent
several, several hours trying to use some info published in "Delphi
Unleashed" (p. 399) regarding "working with Text files". Below is the
code they show for text being written to an output file.
   I would like to do this from  my Windows Application which is also
doing other things such as using FTP to transfer a file.
  I would like status messages such as "connected to...", "login...",
"file sent", etc. to be written to this file in simple text, nothing
fancy.
  Here's the code:

    var
       F: System.Text;
  begin
      System.Assign(F, 'MYFILE.TXT');
      Append(F);   { or Reset(F) or Rewrite(F)}
      WriteLn(F, 'call me Ishmael.');
     System.Close(F);
 end;

  I can see from Delphi Help that System.Text can be/should be
"TextFile" and "System.Assign" can be/should be  "AssignFile, etc.
so I corrected for this although I wonder if there still might need to
be an identifier preceding "TextFile"? Or something included in my
USES clause?

  I thought writing to a  DOS text file from Windows was extremely
difficult (for newbies anyway). The above seems so simple, I was
really pleased to find it.
    Well, after many hours, still no success. I get a message that my
method , an "onclick" menuitem event  is not compatible with Property.
Perhaps the above code might only work in a DOS environment?

  Any help would be greatly appreciated either by email or by post
here. Thanks in advance.

  --Alice
  email: a...@neosoft.com

 

Re:Text output to a log file?


Quote
Alice (a...@neosoft.com) wrote:

<snip>
:   Any help would be greatly appreciated either by email or by post
: here. Thanks in advance.
:  
:   --Alice
:   email: a...@neosoft.com

The following code works fine on my machine:

  F : TextFile;
begin
   AssignFile(F,'FOO.TXT');
   ReWrite(F);
   Writeln(F,'Hello World');
   CloseFile(F);
End;

   /Johan...!

Re:Text output to a log file?


In article <4abbcl$...@pendragon.jsc.nasa.gov>,
   a...@neosoft.com (Alice) wrote:

Quote
>  I thought writing to a  DOS text file from Windows was extremely
>difficult (for newbies anyway). The above seems so simple, I was
>really pleased to find it.

Yes, it is that simple.  It's not a question of a "dos" or "windows" text file
-- a text file is a text file!

Quote
>    Well, after many hours, still no success. I get a message that my
>method , an "onclick" menuitem event  is not compatible with Property.
>Perhaps the above code might only work in a DOS environment?

Well, your error message says nothing about your text file routines -- it says
that something's wrong with your onclick method.  How'd you create your
procedure?  Do this:

1. Create a new app.
2. Place a button on the app (double-click on the button in the component
bar).
3. Double-click on the button on your form; this creates a bland procedure for
what to do when the button is clicked on (the 'onclick' event).
4. Add a 'var' section to this new procedure, and add a textfile declaration
to it.  I.e., between the 'procedure' line and the 'begin' line, add these
lines:
VAR
   myFile : TextFile;
5. Add these lines between the 'begin' and 'end' of the procedure you're
working on:
   assignfile (F, 'MYFILE.TXT');
   append (F);
   writeln (F, 'Call me Ishmael.');
   closefile (F);
6. Run the application.  When you click on that button in the middle of your
form, a text file called myfile.txt should be created (or appended to).

Good luck!
Kendall

Re:Text output to a log file?


In article <4ain38$...@news2.his.com>,
   kend...@his.com (Kendall P. Bullen) wrote:

Quote
>   append (F);

Damn, I thought I'd never follow-up my own article.  Ever!  I pasted code from
the original question, and thus forgot to change this to an append.  (I tested
after I posted, 'cuz Delphi freaked out on my & crashed, which fortunately was
not fatal.)  So anyway, this should be 'rewrite' the first time you call it (I
got an error trying to append to a file that didn't exist ;) -- and then after
it exists, you can 'append' just fine, methinks.

Kendall

Re:Text output to a log file?


Quote
In article <4abbcl$...@pendragon.jsc.nasa.gov> a...@neosoft.com (Alice) writes:
>From: a...@neosoft.com (Alice)
>Subject: Text output to a log file?
>Date: Sat, 09 Dec 1995 08:32:10 GMT
>Newbie here. Sorry if this has already been covered.  I have spent
>several, several hours trying to use some info published in "Delphi
>Unleashed" (p. 399) regarding "working with Text files". Below is the
>code they show for text being written to an output file.
>   I would like to do this from  my Windows Application which is also
>doing other things such as using FTP to transfer a file.
>  I would like status messages such as "connected to...", "login...",
>"file sent", etc. to be written to this file in simple text, nothing
>fancy.
>  Here's the code:
>    var
>       F: System.Text;
>  begin
>      System.Assign(F, 'MYFILE.TXT');
>      Append(F);   { or Reset(F) or Rewrite(F)}
>      WriteLn(F, 'call me Ishmael.');
>     System.Close(F);
> end;
>  I can see from Delphi Help that System.Text can be/should be
>"TextFile" and "System.Assign" can be/should be  "AssignFile, etc.
>so I corrected for this although I wonder if there still might need to
>be an identifier preceding "TextFile"? Or something included in my
>USES clause?
>  I thought writing to a  DOS text file from Windows was extremely
>difficult (for newbies anyway). The above seems so simple, I was
>really pleased to find it.
>    Well, after many hours, still no success. I get a message that my
>method , an "onclick" menuitem event  is not compatible with Property.
>Perhaps the above code might only work in a DOS environment?
>  Any help would be greatly appreciated either by email or by post
>here. Thanks in advance.

>  --Alice
>  email: a...@neosoft.com

I have used similar ideas, with untyped files. Possibly it is to do with the
text file.

But to change the subject, if you want easy, why don't you just use
tstringlist.savetofile('FileX.'). Instead of writing to the messages directly
to the file, you create  a tstringlist, use the add method to put your lines
into the list, then when done,  just call savetofile. works much better - you
can also point a memo or listbox at the stringlist, letting the user see
what's about to go into the file, if you want.

give it a whirl

Boris Ingram, Cyborg Software
boris...@iafrica.com
http://www.pcb.co.za/users/borising/cyborg.htm

Other Threads