From Clomosy Docs

(Created page with "The AssignFile procedure assigns a value for a FileName in preparation for reading or writing to that file.<br> Version 1<br> 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.<br> Version 2<br> Takes a binary file variable type as the handle. The file is treated as a binary file.<br> In both cases, when the f...")
 
No edit summary
 
(5 intermediate revisions by one other user not shown)
Line 1: Line 1:
The AssignFile procedure assigns a value for a FileName in preparation for reading or writing to that file.<br>  
<div class="alert alert-ligth border border-3 border-primary-subtle rounded-5 p-4 shadow-sm" role="alert">
procedure AssignFile(var FileHandle TextFile; const FileName string);
</div>


Version 1<br>
It is used to associate a file variable with a file name, enabling the program to interact with a specific file.<br>
The `AssignFile` function creates a file variable and associates it with the specified file name.<br>
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.<br>
The function takes two parameters: the first parameter is the variable corresponding to the created `TextFile` object, and the second parameter is the name of the file to be created.<br>
Version 2<br>
Takes a binary file variable type as the handle. The file is treated as a binary file.<br>


In both cases, when the file is opened by Append, Reset or ReWrite, it is assumed to be in the current directory.
In both cases, when the file is opened by Append, Reset or ReWrite, it is assumed to be in the current directory.


'''Example:'''<br>
<div class="alert alert-warning" role="alert" data-bs-theme="light">
'''var'''
In the Clomosy Learn application, the file path should be specified as follows for saving files within the project directory.<br>
   myFile : TextFile;<br>
AssignFile(FileHandle, <b>Clomosy.AppFilesPath</b>+'TaskList.txt');
'''begin'''
</div>
 
<b>Example</b><br>
<pre>
var
   myFile : TextFile;
 
{
   // Try to open the Test.txt file for writing to
   // Try to open the Test.txt file for writing to
   '''AssignFile'''(myFile, 'Test.txt');
   AssignFile(myFile, Clomosy.AppFilesPath+'TaskList.txt');
   ReWrite(myFile);<br>
   ReWrite(myFile);
 
  ShowMessage('The file is added to the directory where the exe is located.');
 
   // Write a couple of well known words to this file
   // Write a couple of well known words to this file
   WriteLn(myFile, 'Hello');
   WriteLn(myFile, 'Task 1');
   WriteLn(myFile, 'World');<br>
   WriteLn(myFile, 'Task 2');
  ShowMessage('Lines have been added to the file.');  
   // Close the file
   // Close the file
   CloseFile(myFile);<br>
   CloseFile(myFile);
  // Reopen to append a final line to the file
}
  Append(myFile);<br>
</pre>
  // Write this final line
 
  WriteLn(myFile, 'Final line added');<br>
<b>Output:</b><br>
  // Close the file
After the file has been saved, it appears as follows.<br>
  CloseFile(myFile);<br>
 
  // Reopen the file for reading
[[File:AssignFileExample.png|frameless|500px]]<br>
  Reset(myFile);<br>
 
  // Display the file contents
<h2> See Also </h2>
  while not Eof(myFile) do
* [[File_Handling | File Handling]]
  begin
{{#seo:|title=AssignFile in Clomosy - Clomosy Docs}}
    ReadLn(myFile);
  end;<br>
  // Close the file for the last time
  CloseFile(myFile);<br>
'''end;'''

Latest revision as of 15:14, 24 December 2024

It is used to associate a file variable with a file name, enabling the program to interact with a specific file.
The `AssignFile` function creates a file variable and associates it with the specified file name.
The function takes two parameters: the first parameter is the variable corresponding to the created `TextFile` object, and the second parameter is the name of the file to be created.

In both cases, when the file is opened by Append, Reset or ReWrite, it is assumed to be in the current directory.

Example

var
  myFile : TextFile;

{
  // Try to open the Test.txt file for writing to
  AssignFile(myFile, Clomosy.AppFilesPath+'TaskList.txt');
  ReWrite(myFile);
  
  ShowMessage('The file is added to the directory where the exe is located.'); 
  
  // Write a couple of well known words to this file
  WriteLn(myFile, 'Task 1');
  WriteLn(myFile, 'Task 2');
  ShowMessage('Lines have been added to the file.'); 
  // Close the file
  CloseFile(myFile);
}

Output:
After the file has been saved, it appears as follows.

AssignFileExample.png

See Also