From Clomosy Docs

No edit summary
No edit summary
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">
In the Clomosy Learn application, the file path should be specified as follows for saving files within the project directory.<br>
AssignFile(FileHandle, <b>Clomosy.AppFilesPath</b>+'TaskList.txt');
</div>


:'''TRObject Syntax'''
<b>Example</b><br>
  var
<b>TRObject Syntax</b><br>
    myFile : TextFile;
<pre>
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
    // Try to open the Test.txt file for writing to
  WriteLn(myFile, 'Task 1');
    AssignFile(myFile, Clomosy.AppFilesPath+'TaskList.txt');
  WriteLn(myFile, 'Task 2');
    ReWrite(myFile);
  ShowMessage('Lines have been added to the file.');  
   
  // Close the file
    ShowMessage('The file is added to the directory where the exe is located.');
  CloseFile(myFile);
   
}
    // Write a couple of well known words to this file
</pre>
    WriteLn(myFile, 'Task 1');
<b>Base Syntax</b><br>
    WriteLn(myFile, 'Task 2');
<pre>
    ShowMessage('Lines have been added to the file.');  
var
    // Close the file
   myFile : TextFile;
    CloseFile(myFile);
   }


:'''Base Syntax'''
begin
   var
  // Try to open the Test.txt file for writing to
    myFile : TextFile;
  AssignFile(myFile, Clomosy.AppFilesPath+'TaskList.txt');
  ReWrite(myFile);
    
  ShowMessage('The file is added to the directory where the exe is located.');  
    
    
   begin
   // Write a couple of well known words to this file
    // Try to open the Test.txt file for writing to
  WriteLn(myFile, 'Task 1');
    AssignFile(myFile, Clomosy.AppFilesPath+'TaskList.txt');
  WriteLn(myFile, 'Task 2');
    ReWrite(myFile);
  ShowMessage('Lines have been added to the file.');  
   
  // Close the file
    ShowMessage('The file is added to the directory where the exe is located.');
  CloseFile(myFile);
   
end;
    // Write a couple of well known words to this file
</pre>
    WriteLn(myFile, 'Task 1');
 
    WriteLn(myFile, 'Task 2');
<b>Output:</b><br>
    ShowMessage('Lines have been added to the file.');  
After the file has been saved, it appears as follows.<br>
    // Close the file
    CloseFile(myFile);
  end;


'''Output:'''<br>
[[File:AssignFileExample.png|frameless|500px]]<br>
After the file has been saved, it appears as follows.


[[File:AssignFileExample.png|frameless|500px]]
<h2> See Also </h2>
* [[File_Handling | File Handling]]

Revision as of 08:57, 23 October 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
TRObject Syntax

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);
}

Base Syntax

var
  myFile : TextFile;

begin
  // 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);
end;

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

AssignFileExample.png

See Also