Text

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

Text replace - "delphi>" to "syntaxhighlight>"

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

{$mode objfpc}{$H+}
var 
  MyFile: TextFile;
  S: string;
begin
  AssignFile(MyFile, 'a.txt');
  Reset(MyFile); 
  try
    Readln(MyFile, S);
  finally
    CloseFile(MyFile);
  end;
end.

The variable representing the text file (MyFile in the example above) may be used to input, output, or both, to the actual file. It must be tied to the actual file by a run-time library routine AssignFile (older name: just Assign). 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 (and possibly flush the data to be written) by closing the file, calling CloseFile (older name: just Close).

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