From Free Pascal wiki
Revision as of 17:15, 13 March 2012 by Michalis (talk | contribs) (This page was largely a copy of (invalid and messy) old Text page. "file of char" is NOT the same as Text/TextFile. Also, no need for unnecessary upper case. "file" is a type, not variable.)

In a Pascal program, a variable of type file may be used to read or write (or both) to the file. A file is (usually) associated with a file on a disk, so it's contents are obviously stored on the disk even after the program terminates.


<delphi>var F: file;</delphi>

Where F is the file variable used to access the file. The word file itself is a reserved word in Pascal.

When a file is defined in this fashion, it is called an untyped file; it may be used to store essentially anything.

To read and write text files, use the Text (more modern name: TextFile) type.

A file variable is used either for input, for output, or for both input and output (I/O). A file may be a screen and/or keyboard, a modem, a light pen, a touch screen, a mouse, an area of storage on disk, a socket for connecting to the Internet, or it could be some other device depending on how the run-time library ties the file. In some cases, actual memory can be defined as a file; this was used in the case of a ram disk.

Some of these devices are connected through various specialized libraries instead of treating them as actual files. For example, databases are not normally accessed directly as a simple file which is read or written, they are usually accessed either through a set of database handling procedures and functions in an external unit or library, or are accessed via SQL commands sent to a procedure or function. Programs that communicate with the Internet usually use a set of routines called sockets under Unix-based operating systems, or using the Winsock routines on Microsoft Windows, which essentially provide the same functions as the Unix sockets routines.

Generally, the standard Pascal language does not distinguish between types of files, treating all files the same, allowing them to be accessed via the read, readln, write and writeln standard procedures. Some Pascal Compilers also used the Get and Put routines for I/O.

Enhanced versions of Pascal such as UCSD Pascal, Turbo Pascal, and, of course, FPC Pascal have added features to indicate that a file is specifically a screen or a disk file, and to allow for such functions as naming of disk files, random access, appending to the end and deleting of files as needed.

File-related types, procedures and functions:

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