AssignFile
Belongs to : ProcedureDescription
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.
Example code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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; { Hello World } |