Accessing Files Programmer's Guide (32650-90885)

Chapter 9 131
Reading from a File
Sequential Access and Random Access
end_of_file := false; {initialize exit condition }
expected_length := -256; {record size of file }
control_code := 0 {set to default }
repeat {begin loop }
record_length := FREAD ( disk_file, {file number from HPFOPEN }
record, {data transferred to here }
expected_length {record size of file }
);
if ccode = ccl then handle_file_error (disk_file) {error check }
else if ccode = ccg then end_of_file ;= true {exit condition check}
else begin
FWRITE( new_file, {file number from HPFOPEN }
record, {data transferred here }
record_length, {value returned by FREAD }
control_code {required; set to default }
);
end
until end_of_file; {loop ends when exit condition encounted }
.
.
.
If an error is encountered by either FREAD or FWRITE, the condition code CCL is returned to
the program, thus invoking the procedure handle_file_error. For more information
about FREAD parameters, refer to the MPE/iX Intrinsics Reference Manual. For more
information about using the FWRITE intrinsic, refer to chapter 8, "Writing to a File". For
more information about opening a file, refer to chapter 5, "Opening a File".
Reading from a disk file using random access
Example 9-2 is an HP Pascal/iX code segment that, within a loop construct, calls the
FREADDIR intrinsic to read a record whose record number has been selected by the
procedure select_record and returned in the variable record_number. The example
then prints the selected record to the standard list device $STDLIST using the PRINT
intrinsic.
Example 9-2. Reading from a Disk File Using Random Access
.
.
.
var
record : packed array [1..30] of char; {declare record type }
record_length : shortint; {expected record length }
read_length : shortint; {actual bytes read by FREAD}
record_number : integer; {which record to read }
control_code : shortint; {required by PRINT }
end_of_file : boolean; {declare exit condition }
.
.
.
control_code := 0; {default condition }