Sign in Join
Wiki copytemporary

Text

English · Русский · 日本語

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

pascal
{$mode objfpc}{$H+}
var 
  MyFile: TextFile;
  s: string;
begin
  AssignFile(MyFile, 'a.txt');

  try
    reset(MyFile);    //Reopen the file for reading
    readln(MyFile, s);
    writeln('Text read from file: ', s) 
   
    {
    or add some text:
    append(MyFile);
    writeln(MyFile, 'some text'); 
    }

  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:


From the Free Pascal wiki, page "Text", as it stood on 2020-02-29 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.