Most file variables have an associated buffer variable, which provides direct access to the next value waiting to be read from the file, or the next value to be written to the file.
In Standard Pascal (i.e. ISO/IEC 7185), file variables can be opened in one of two modes:
When a file variable is opened in read-only mode, the contents of the buffer variable associated with the file variable, will either be equal to the first value in the file or be undefined if the file is empty. As the file is read, the contents of the buffer variable will always either be equal to the next value to be read from the file, or be undefined if there is no next value to be read from the file (i.e. the entire file has been read). Standard Pascal allows an implementation to delay the point at which the next value to be read from the file, is actually transferred into the buffer variable, until the buffer variable is referenced by the program. This process is a part of what is called lazy I/O, where file output operations take place as soon as possible, but file input operations are delayed. Lazy I/O is convenient for interactive programs, where it is important that output operations, displaying messages prompting the user to input values, take place before input operations that actually try to read values from the user (so that the user knows what he/she is expected to type in). Irie Pascal implements lazy I/O.When a file variable is opened in write-only mode, the contents of the buffer variable associated with the file variable is initially undefined. You can write values to the file associated with the file variable, by assigning values to the buffer variable associated with the file variable, and using the built-in procedure put to write the contents of the buffer variable to the file.
Irie Pascal allows file variables to be opened in read/write mode, where values can be both read from, and written to the files associated with the file variables. If you reference the buffer variable of a file variable opened in read/write mode, it is unclear whether you intend to read from the file (in which case the next available value from the file should be transferred to the buffer variable), or whether you intend to write to the file (in which case no I/O should take place). Because of this uncertainty it is not recommended that you use buffer variables and the low-level I/O procedures, get and put, on file variable opened in read/write mode. Instead it is recommended that you use the high-level I/O procedures read, write, etc to read and write from file variables opened in read/write mode.
Buffer variables are created and destroyed when their associated file variables are created and destroyed.
The simple example program below uses a file variable (f) to create a file called numbers.dat and uses the associated buffer variable (f) to write the numbers 1 to 10 to the file.
program numbers;
var
f : file of integer;
i : integer;
begin
assign(f, 'numbers.dat');
rewrite(f);
for i := 1 to 10 do
begin
f^ := i;
put(f)
end
end.
The syntax for referencing buffer variables is given below:
(NOTE: for clarity some parts of the syntax are omitted, see Irie Pascal Grammar for the full syntax):
buffer-variable = file-variable '^' | file-variable '@'
Where file-variable is a reference to a file variable.