ObjectPascal
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
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;
Show full unit code
   Hello
   World