SmartPascal
Append
Procedure
Open a text file to allow appending of text to the end System unit
 procedure Append ( var FileHandle : TextFile ) ;
Description
The Append procedure opens a file given by FileHandle to allow subsequent writes to append to the end of the file.
 
You must use AssignFile to assign a file to the FileHandle before using Append.
 
This is often used when logging information - existing log data must be preserved, with the new log information appended to the end.
 
Use Write or WriteLn to write to the file after this Append is executed.
Related commands
AssignFile Assigns a file handle to a binary or text file
TextFile Declares a file type for storing lines of text
Write Write data to a binary or text file
WriteLn Write a complete line of data to a text file
 
Example code : Appending a line of text to an existing file
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  // The System unit does not need to be defined
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  myFile : TextFile;
  text   : string;

begin
  // 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) do
  begin
    ReadLn(myFile, text);
    ShowMessage(text);
  end;

  // Close the file for the last time
  CloseFile(myFile);
end;
 
end.
Hide full unit code
   Hello
   World
   Final line added