Name
AssignFile Procedure
Syntax
procedure AssignFile(var F: File; const FileName: string); procedure AssignFile(var F: TextFile; const FileName: string);
Description
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.
Tips and Tricks
A subsequent call to
Append
,Reset
, orRewrite
will open the file. If you do not callAssignFile
first, a call toAppend
,Reset
, orRewrite
causes Delphi to report I/O error 102.Delphi interprets an empty string as the console. In a console application, the
Input
andOutput
files are automatically assigned to the console. Trying to use a console file in a GUI application results in I/O error 105.
Example
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;
See Also
Append Procedure, CloseFile Procedure, Eof Function, File Type, IOResult Function, Reset Procedure, Rewrite Procedure, TextFile Type, $I Compiler Directive, $IOChecks Compiler Directive |
Get Delphi in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.