Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Write to a text file

Posted on 2003-02-19
5
Medium Priority
?
167 Views
Last Modified: 2010-04-04
I need sample source code for a procedure in Delphi 5 that will accept as a parameter a string, and then it will append that string to a text file.

The text file when opened will look like this

log messeage 1
log message 2
log message 3
...
...
log message 4


where log message will be replaced by whatever custom text I pass into the file.
0
Comment
Question by:Tom Knowlton
  • 3
5 Comments
 
LVL 9

Expert Comment

by:mocarts
ID: 7984109
load file into TStringList (StringList.LoadFromFile(..)) and do all your job on it. when finished SaveToFile(..)
wbr, mo.
0
 
LVL 9

Expert Comment

by:mocarts
ID: 7984141
thats if you want operation on file content..
if you need simply to append then:
function AppendStringToFile(const FileName, StringToAppend; string): boolean;
var
  tf: TextFile;
begin
  try
    Assign(tf, FileName);
    if not FileExists(FileName) then Rewrite(tf)
      else  Reset(tf);
  try
    WriteLn(tf, StringToAppend);
  finally
    CloseFile(tf);
  end;
  except
    Result := false;
  end;
end;

if you will use this fn frequently then it's better to initialize tf  once (somwhere in main form's OnCreate (and CloseFile(tf) in OnDestroy of course))
wbr, mo.
0
 
LVL 17

Accepted Solution

by:
geobul earned 400 total points
ID: 7984238
Hi,

I'm sorry, mocarts but I think that your example is wrong.
1. The function has no result set to true in case of success.
2. The function will try to replace the first line in the file instead of adding a new line to it.

My proposal is:

function AppendStringToFile(const FileName, StringToAppend: string): boolean;
var
 tf: TextFile;
begin
 result := true;
 try
   Assign(tf, FileName);
   if not FileExists(FileName) then Rewrite(tf)
     else  Append(tf);
   try
     WriteLn(tf, StringToAppend);
   finally
     CloseFile(tf);
   end;
 except
   Result := false;
 end;
end;

// usage:
AppendStringToFile('c:\test.txt', Edit1.Text);

Regards, Geo
0
 
LVL 9

Expert Comment

by:mocarts
ID: 7986162
yes, Geo, you are right :)
I wasn't tested this - simply write here in textbox.
I prefer to work with CreateFile and etc. not with Delphi's Assing(..) and that's why so many mistakes.
mo.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 7988907
Thank you both!

Tom
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

581 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