Open a text or binary file for write access System unit
1  procedure ReWrite ( var FileHandle : TextFile ) ;
2  procedure ReWrite ( var FileHandle : File {RecordSize ) ;
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
 Author links

 Download this web site as a Windows program.

Example code : Writing and reading lines of text to/from a text file
  myFile : TextFile;
  text   : string;

  // Try to open the Test.txt file for writing to
  AssignFile(myFile, 'Test.txt');

  // Write a couple of well known words to this file
  WriteLn(myFile, 'Hello');
  WriteLn(myFile, 'World');

  // Close the file

  // Reopen the file in read only mode

  // Display the file contents
  while not Eof(myFile) do
    ReadLn(myFile, text);

  // Close the file for the last time
Show full unit code
Delphi Programming Neil Moffatt 2002 - 2020. All rights reserved.  |  Home Page