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
Line 12: Line 12:


'''Example:'''<br>
'''Example:'''<br>
'''var'''
:'''Base Syntax'''
   myFile : TextFile;<br>
   var
'''begin'''
  myFile : TextFile;
  // Try to open the Test.txt file for writing to
    
  '''AssignFile'''(myFile, 'Test.txt');
  ReWrite(myFile);<br>
  // Write a couple of well known words to this file
  WriteLn(myFile, 'Hello');
  WriteLn(myFile, 'World');<br>
  // Close the file
  CloseFile(myFile);<br>
  // Reopen to append a final line to the file
  Append(myFile);<br>
   // Write this final line
  WriteLn(myFile, 'Final line added');<br>
  // Close the file
  CloseFile(myFile);<br>
  // Reopen the file for reading
  Reset(myFile);<br>
  // Display the file contents
  while not Eof(myFile) do
   begin
   begin
    ReadLn(myFile);
  // Try to open the Test.txt file for writing to
  end;<br>
  '''AssignFile'''(myFile, 'Test.txt');
  // Close the file for the last time
  ReWrite(myFile);
  CloseFile(myFile);<br>
 
  '''end;'''
  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, 'Hello');
  WriteLn(myFile, 'World');
  ShowMessage('Lines have been added to the file.');
  // 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) do
  begin
  ReadLn(myFile);
  end;
 
  // Close the file for the last time
  CloseFile(myFile);
  end;
 
:'''TRObject Syntax'''
var
  myFile : TextFile;
   
{
  // Try to open the Test.txt file for writing to
  '''AssignFile'''(myFile, 'Test.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, 'Hello');
  WriteLn(myFile, 'World');
  ShowMessage('Lines have been added to the file.');
  // 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 12:33, 7 February 2024

The AssignFile procedure assigns a value 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.

Example:

Base Syntax
 var
  myFile : TextFile;
 
 begin
  // Try to open the Test.txt file for writing to
  AssignFile(myFile, 'Test.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, 'Hello');
  WriteLn(myFile, 'World');
  ShowMessage('Lines have been added to the file.'); 
  // 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) do
  begin
  ReadLn(myFile);
  end;
  
  // Close the file for the last time
  CloseFile(myFile);
 end;
TRObject Syntax
var
  myFile : TextFile;

{
  // Try to open the Test.txt file for writing to
  AssignFile(myFile, 'Test.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, 'Hello');
  WriteLn(myFile, 'World');
  ShowMessage('Lines have been added to the file.'); 
  // 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);
}