Writing And Reading From a TextFile Delphi

Writing And Reading From a TextFile Delphi

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.

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

  // 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;