AssignFile Procedure
procedure AssignFile(var F: File; const FileName: string); procedure AssignFile(var F: TextFile; const FileName: string);
Call
AssignFile
to assign a filename to a typed file,
an untyped file, or a text file prior to opening the file.
AssignFile
is not a real procedure.
A subsequent call to Append
,
Reset
, or Rewrite
will open the
file. If you do not call AssignFile
first, a call
to Append
, Reset
, or
Rewrite
causes Delphi to report I/O error 102.
Delphi interprets an empty string as the console. In a console
application, the Input
and
Output
files are automatically assigned to the
console. Trying to use a console file in a GUI application results in
I/O error 105.
var
LogFile: string = 'c:log.txt';
// Append a message to a log file. See the example with the Array
// Keyword for the other overloaded Log procedure.
procedure Log(const Msg: string); overload;
var
F: TextFile;
begin
AssignFile(F, LogFile);
// Try to append to the file, which succeeds only if the file exists.
{$IoChecks Off}
Append(F);
{$IoChecks On}
if IOResult <> 0 then
// The file does not exist, so create it.
Rewrite(F);
WriteLn(F, Msg);
CloseFile(F);
end;