Mega Code Archive

 
Categories / Delphi / Files
 

Adding Text to Log Files

Title: Adding Text to Log Files Question: How to add a line of text to the log file without using memos or other visual components? Answer: Use this function: function AddTextToFile(const aFileName, aText: string; AddCRLF: Boolean): Boolean; var lF: Integer; lS: string; begin Result := False; if FileExists(aFileName) then lF := FileOpen(aFileName, fmOpenWrite + fmShareDenyNone) else lF := FileCreate(aFileName); if (lF = 0) then try FileSeek(lF, 0, 2); if AddCRLF then lS := aText + #13#10 else lS := aText; FileWrite(lF, lS[1], Length(lS)); finally FileClose(lF); end; end;