Sign in Join
Wiki copytemporary

Text: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Text

OldNewLine
1+The type **TextFile** (or, equivalent and older, just **Text**) is used in a Pascal program to read from and write to a text file.
2+
3+``` pascal
4+{$mode objfpc}{$H+}
5+var
6+ MyFile: TextFile;
7+ s: string;
8+begin
9+ AssignFile(MyFile, 'a.txt');
10+
11+ try
12+ reset(MyFile); //Reopen the file for reading
13+ readln(MyFile, s);
14+ writeln('Text read from file: ', s)
15+
16+ {
17+ or add some text:
18+ append(MyFile);
19+ writeln(MyFile, 'some text');
20+ }
21+
22+ finally
23+ CloseFile(MyFile)
24+ end
25+end.
26+```
27+
28+The [[Variable|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 [[RTL|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]].
29+
30+Note that **TextFile** type is very different than the **file of char** type:
31+
32+- **file of char** is just a simple sequence of single byte characters and you can only read or write a single character at a time. That is, you can only call **Read(F, C)** or **Write(F, C)** where C is variable of type **char**.
33+
34+- **TextFile** offers much more functions, and represents the usual concept of a text file. You can use Read, Readln, Write, Writeln to read/write from a text file a number of standard types, like strings, integers and floating-point values. [[End of Line|Line endings]] are also automatically handled: when reading, various line endings are recognized; when writing, the current OS line endings are used.
35+
36+---
37+*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](/wiki-contact).*

Back to history