From Clomosy Docs
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
<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> | |||
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> | |||
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> | |||
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. | ||
'' | <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> | |||
<b>Example</b><br> | |||
<b>TRObject Syntax</b><br> | |||
<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 | |||
WriteLn(myFile, 'Task 1'); | |||
WriteLn(myFile, 'Task 2'); | |||
ShowMessage('Lines have been added to the file.'); | |||
// Close the file | |||
CloseFile(myFile); | |||
} | |||
</pre> | |||
<b>Base Syntax</b><br> | |||
<pre> | |||
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; | |||
</pre> | |||
<b>Output:</b><br> | |||
After the file has been saved, it appears as follows.<br> | |||
[[File:AssignFileExample.png|frameless|500px]]<br> | |||
[[File | <h2> See Also </h2> | ||
* [[File_Handling | File Handling]] | |||
Revision as of 08:57, 23 October 2024
procedure AssignFile(var FileHandle TextFile; const FileName string);
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.
In the Clomosy Learn application, the file path should be specified as follows for saving files within the project directory.
AssignFile(FileHandle, Clomosy.AppFilesPath+'TaskList.txt');
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.