Assigns a file handle to a binary or text file System unit
1  procedure AssignFile ( var FileHandle : TextFile; const FileName : string ) ;
2  procedure AssignFile ( var FileHandle : File; const FileName : string ) ;
The AssignFile procedure assigns a value to FileHandle for a FileName in preparation for reading or writing to that file.
Version 1
Takes a text file variable type as the handle. The file is treated as a textfile when opened.
If the file name is an empty string, then file access is made to the console standard input and output streams.
Version 2
Takes a binary file variable type as the handle. The file is treated as a binary file.
In both cases, when the file is opened by Append, Reset or ReWrite, it is assumed to be in the current directory.
The FileHandle must no be confused with the file handle used for the low level file handling routines such as FileOpen and FileRead.
Related commands
Append Open a text file to allow appending of text to the end
AssignPrn Treats the printer as a text file - an easy way of printing text
File Defines a typed or untyped file
Reset Open a text file for reading, or binary file for read/write
ReWrite Open a text or binary file for write access
TextFile Declares a file type for storing lines of text
Example code : Write to a text file, and then read back its contents
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
unit Unit1;
  // The System unit does not need to be defined
  Forms, Dialogs;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
Form1: TForm1;
{$R *.dfm} // Include form definitions
procedure TForm1.FormCreate(Sender: TObject);

  myFile : TextFile;
  text   : string;

  // Try to open the Test.txt file for writing to
  AssignFile(myFile, 'Test.txt');

  // Write a couple of well known words to this file
  WriteLn(myFile, 'Hello');
  WriteLn(myFile, 'World');

  // Close the file

  // Reopen the file for reading

  // Display the file contents
  while not Eof(myFile) do
    ReadLn(myFile, text);

  // Close the file for the last time
Hide full unit code