Declares a file type for storing lines of text System unit
  var TextFile;
The TextFile type defines a file type for holding textual data.
Text files provide a simple, convenient way of storing textual data. They do provide mechanisms for reading and writing numerical data stored as text (see Write), but it is safer and wiser to use structured records when storing anything other than plain text strings.
However, text files do allow variable length records.
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
File Defines a typed or untyped file
Reset Open a text file for reading, or binary file for read/write
ReWrite Open a text or binary file for write access
 Author links

 Buy Website Traffic at

 Buy Proxies at
 Download this web site as a Windows program.

Example code : Writing to and reading from a TextFile
// 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;
  // The System unit does not need to be defined
  Forms, Dialogs;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
Form1: TForm1;
{$R *.dfm} // Include form definitions
procedure TForm1.FormCreate(Sender: TObject);

  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 World');

  // Close the file

  // Reopen the file for reading

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

  // Close the file for the last time
Hide full unit code
   Hello World
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page