procedure RecordLog(Log: string); var f: Textfile; FileName: string; begin FileName := 'c:\a.log'; AssignFile(f, FileName); try if FileExists(FileName) = False then Rewrite(f) else begin Reset(f); Append(f); end; Writeln(f, FormatDateTime('yyyy-mm-dd hh:nn:ss ', Now) + Log); finally CloseFile(f); end; end;RecordLog('用户登陆');
用操作Text的方法来实现,Delphi帮助中示例: var F1: TextFile; begin if OpenDialog1.Execute then begin AssignFile(F1, OpenDialog1.Filename); Reset(F1); Write(F2, '登录成功'); CloseFile(F1); end; end; 每次用Append方法 var f: TextFile; begin if OpenDialog1.Execute then begin { open a text file } AssignFile(f, OpenDialog1.FileName); Append(f); Writeln(f, 'I am appending some stuff to the end of the file.'); CloseFile(f); end; end;
var
f: Textfile;
FileName: string;
begin
FileName := 'c:\a.log';
AssignFile(f, FileName);
try
if FileExists(FileName) = False then
Rewrite(f)
else
begin
Reset(f);
Append(f);
end;
Writeln(f, FormatDateTime('yyyy-mm-dd hh:nn:ss ', Now) + Log);
finally
CloseFile(f);
end;
end;RecordLog('用户登陆');
var
F1: TextFile;
begin
if OpenDialog1.Execute then begin
AssignFile(F1, OpenDialog1.Filename);
Reset(F1);
Write(F2, '登录成功');
CloseFile(F1);
end;
end;
每次用Append方法
var
f: TextFile;
begin
if OpenDialog1.Execute then
begin { open a text file }
AssignFile(f, OpenDialog1.FileName);
Append(f);
Writeln(f, 'I am appending some stuff to the end of the file.');
CloseFile(f);
end;
end;