HP Pascal/iX Programmer's Guide (31502-90023)
3- 20
before referencing the buffer implicitly (with a sequential I/O
procedure) or explicitly.
Table 3-9 summarizes the characteristics of the predefined direct
file functions.
Table 3-9. Characteristics of Direct File Functions
----------------------------------------------------------------------------------------
| | | | |
| Function |
Lastpos
|
Maxpos
|
Eof
|
| | | | |
----------------------------------------------------------------------------------------
| | |
| State that | Read-write |
| file must be | |
| in | |
| | |
----------------------------------------------------------------------------------------
| | | | |
| Returns | Position number of | Position number of | Returns
true
if |
| | highest-numbered | highest-numbered | current position |
| | component that you | component that you | index is after |
| | can read (the last | can write | lastpos;
false
|
| | component ever | | otherwise |
| | written) | | |
| | | | |
----------------------------------------------------------------------------------------
All of the sequential file functions work the same way on direct files,
except for a subtle difference in the eof function (compare Table 3-5
and Table 3-9 ).
Example 2
PROGRAM prog;
TYPE
cfile = FILE OF char;
VAR
f : cfile;
c : char;
BEGIN
reset(f); {Opens file for sequential input.}
WHILE not(eof(f)) DO read(f,c); {Reads until eof is true.}
read(f,c); {ERROR -- cannot read when eof is true.
This statement would abort the program.}
open(f); {Opens file for direct input/output.}
IF lastpos(f) < maxpos(f) THEN BEGIN
seek(f,lastpos(f)+1); {Puts current position index beyond
last component, making eof true.}
read(f,c); {ERROR -- cannot read beyond lastpos(f).}
write(f,c); {Writes beyond last component.
The component written becomes the last.}
END;
END.
Closing Files
When your program closes a file, it breaks the association between the
logical file and the physical file; therefore, it cannot access the file
or file buffer variable. It must reopen the file before attempting to
operate on it in any other way, or it is a run-time error. One way to
close a file is with the predefined procedure close. A call to close has