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>
The CloseFile procedure closes an open file. The file must be assigned and opened with Insert, Reset, or Rewrite. The file is closed and the handle is made available for further assignment to the files.<br>
The CloseFile procedure closes an open file. The file must be assigned and opened with Insert, Reset, or Rewrite. The file is closed and the handle is made available for further assignment to the files.<br>


'''Example:'''<br>
<b>Example</b><br>
:'''Base Syntax'''
<pre>
  '''var'''
  var
   myFile : TextFile;<br>
   myFile : TextFile;
  '''begin'''
   
{
   // 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, 'Test.txt');
   ReWrite(myFile);<br>
   ReWrite(myFile);
   // 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, 'Hello');
   WriteLn(myFile, 'World');<br>
   WriteLn(myFile, 'World');
   // Close the file
   // Close the file
   '''CloseFile'''(myFile);<br>
   CloseFile(myFile);
   // Reopen to append a final line to the file
   // Reopen to append a final line to the file
   Append(myFile);<br>
   Append(myFile);
   // Write this final line
   // Write this final line
   WriteLn(myFile, 'Final line added');<br>
   WriteLn(myFile, 'Final line added');
   // Close the file
   // Close the file
   CloseFile(myFile);<br>
   CloseFile(myFile);
   // Reopen the file for reading
   // Reopen the file for reading
   Reset(myFile);<br>
   Reset(myFile);
   // Display the file contents
   // Display the file contents
   while not Eof(myFile) do
   while (not Eof(myFile))
  begin
     ReadLn(myFile);
     ReadLn(myFile);
  end;<br>
   // Close the file for the last time
   // Close the file for the last time
   CloseFile(myFile);<br>
   CloseFile(myFile);
'''end;'''
}
</pre>


:'''TRObject Syntax'''
<h2> See Also </h2>
  var
* [[File_Handling | File Handling]]
  myFile : TextFile;
 
  {
  // 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 to append a final line to the file
  Append(myFile);
 
  // Write this final line
  WriteLn(myFile, 'Final line added');
 
  // Close the file
  CloseFile(myFile);
 
  // Reopen the file for reading
  Reset(myFile);
 
  // Display the file contents
  while (not Eof(myFile))
    ReadLn(myFile);
 
  // Close the file for the last time
  CloseFile(myFile);
 
  }

Revision as of 11:05, 23 October 2024

The CloseFile procedure closes an open file. The file must be assigned and opened with Insert, Reset, or Rewrite. The file is closed and the handle is made available for further assignment to the files.

Example

 var
  myFile : TextFile;
 
 {
  // 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 to append a final line to the file
  Append(myFile);
 
  // Write this final line
  WriteLn(myFile, 'Final line added');
 
  // Close the file
  CloseFile(myFile);
 
  // Reopen the file for reading
  Reset(myFile);
 
  // Display the file contents
  while (not Eof(myFile))
    ReadLn(myFile);
 
  // Close the file for the last time
  CloseFile(myFile);
 
 }

See Also