Accessing Files Programmer's Guide (32650-90885)

132 Chapter9
Reading from a File
Sequential Access and Random Access
record_length := -30; {file record size 30 bytes }
record_number := 0; {initialize variable }
end_of_file := false; {initialize exit condition }
repeat {begin loop }
select_record (record_number);
read_length := FREADDIR (data_file, {HPFOPEN file number }
record, {record read from data_file }
record_length,{expected length of record }
record_number {returned from select_record }
);
if ccode = ccl then handle_file_error (data_file) {error check }
else if ccode = ccg then end_of_file := true {check for exit condition}
else begin
PRINT (record, {returned by FREADDIR }
read_length, {returned by FREADDIR }
control_code {set to default condition}
);
if ccode <> cce then handle_file_error (data_file)
end
until end_of_file; {exit if exit condition true }
.
.
.
Assume that a disk file identified by data_file has been opened elsewhere by an
HPFOPEN/FOPEN call. Also, assume that procedure select_record prompts the user for a
valid record number of a record in data_file. The loop is repeated until the FREADDIR
intrinsic encounters an end-of-file condition, or an error condition is returned by an
intrinsic.
If an error is encountered by either FREADDIR or PRINT, procedure handle_file_error is
invoked. For more information about FREADDIR parameters, refer to theMPE/iX Intrinsics
Reference Manual. For more information about using the PRINT intrinsic, refer to chapter
8, "Writing to a File". For more information about opening a file, refer to chapter 5,
"Opening a File".
Increasing I/O performance using FREADSEEK
If you know in advance that a certain record is to be read from a file with the FREADDIR
intrinsic, you can speed up the I/O process by issuing an FREADSEEK intrinsic call.
The FREADSEEK intrinsic moves the record from disk to virtual memory. Then, when the
FREADDIR intrinsic call is issued, the record is transferred from virtual memory to the
buffer in the stack specified by FREADDIR without having to perform I/O. The use of
FREADSEEK enhances the I/O process, because the FREADDIR call does not make the file
system perform a physical I/O.