SmartPascal
ReWrite
Procedure
Open a text or binary file for write access System unit
1  procedure ReWrite ( var FileHandle : TextFile ) ;
2  procedure ReWrite ( var FileHandle : File {RecordSize ) ;
Description
The ReWrite procedure opens a file given by FileHandle for writing.
 
You must use AssignFile to assign a file to the FileHandle before using Reset.
 
If the file does not exist, it is created.
 
If the file already exists, the contents are lost, and new data is added to the start.
 
Use Write or WriteLn to write to the file after this ReWrite is executed.
 
Version 1
 
Is used for text files.
 
Version 2
 
Is for binary files. The optional RecordSize value is used to override the default 128 byte record size for binary (untyped) files. For such files, only BlockRead and BlockWrite can be used.
Related commands
Append Open a text file to allow appending of text to the end
AssignFile Assigns a file handle to a binary or text file
CloseFile Closes an open file
File Defines a typed or untyped file
Reset Open a text file for reading, or binary file for read/write
TextFile Declares a file type for storing lines of text
 
Example code : Writing and reading lines of text to/from a text 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 the file in read only mode
  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