In this article, for the sake of ease of understanding, reserved words in Pascal are shown in UPPER CASE even though the Pascal lanugage is not case sensitive.
When used in a Pascal program, a file is a variable which may be used to retrieve data from by reading the file variable, or to send data to the file variable, or for both reading and writing. A file differs from normal variables in a program in that the information within the file is usually available even after the program terminates.
The Pascal source format is
- VAR filname : FILE;
Where filname 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.
- VAR filname : FILE OF char;
- VAR filname : text;
In the above example, the identifier used (filname) is the file variable which is used to do input, output, or both, to the actual file. The file variable, however, is not the actual file; the file variable must be tied to the actual file by a run-time library routine. In most cases, this is done via the assign procedure followed by use of the reset or rewrite procedure. In the case of specialized files such as databases, it is done via some other method than the standard ones, in order to allow a file variable to actually read from and/or write to the actual file itself using specialized routines.
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: