?
Solved

Write to a text file

Posted on 2003-02-19
5
Medium Priority
?
162 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
[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
  • 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

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

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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

770 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