Text

This is an old revision of Text, edited by Tudi x at . The current revision is 31442.

The type TextFile (or, equivalent and older, just Text) is used in a Pascal program to read from and write to a text file.

{$mode objfpc}{$H+}
var 
  MyFile: TextFile;
  s: string;
begin
  AssignFile(MyFile, 'a.txt');
  try
    reset(MyFile); 
    readln(MyFile, s);
    writeln(MyFile, 'Text read from file: ', s)

  finally
    CloseFile(MyFile)
  end
end.

The variable representing the text file (MyFile in the example above) may be used to read from, write to, or both to the actual file. It must be tied to the actual file by a run-time library routine AssignFile. Then the file must be opened by the Reset, Rewrite or Append procedure. You can read and write to file using Read, Readln, Write, Writeln. After you have finished processing the file, you should release the necessary file resources by closing the file calling CloseFile.

Note that TextFile type is very different than the file of char type:

File-related types, procedures and functions:

File - Text - AssignFile - CloseFile - Reset - Rewrite - Get - Put - Read - Readln - Write - Writeln