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
AppendOpen a text file to allow appending of text to the end
AssignPrnTreats the printer as a text file - an easy way of printing text
FileDefines a typed or untyped file
ResetOpen a text file for reading, or binary file for read/write
ReWriteOpen a text or binary file for write access
TextFileDeclares a file type for storing lines of text
 Download this web site as a Windows program.

Example code : Write to a text file, and then read back its contents
  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
Show full unit code
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page