SmartPascal
AssignFile
Procedure
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 ) ;
Description
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.
Notes
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;
 
interface
 
uses
  // The System unit does not need to be defined
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  myFile : TextFile;
  text   : string;

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

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

  // Close the file
  CloseFile(myFile);

  // Reopen the file for reading
  Reset(myFile);

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

  // Close the file for the last time
  CloseFile(myFile);
end;
 
end.
Hide full unit code
   Hello
   World