Accessing Files Programmer's Guide
> Chapter 9 Reading from a FileSequential Access and Random Access |
||||||||||||||||||||||
|
Reading from a disk file using sequential accessExample 9-1 is an HP Pascal/iX code segment that uses the FREAD intrinsic to read records sequentially from a disk file. Example 9-1 contains a loop construct, where records are read sequentially from disk_file and written to the file new_file (both files opened elsewhere by HPFOPEN/FOPEN calls). The files are both standard ASCII files with fixed-record format, each record 256 bytes in length. When a logical end-of-file (EOF) is reached, a condition code of CCG is returned by FREAD. The loop ends when FREAD encounters the EOF and returns the CCG condition to the program. Example 9-1. Reading from a Disk File Using Sequential Access . . . var expected_length: shortint; {required by FREAD} record : packed array [1..256] of char;{declare rec type} control_code : 0..65535; {required by FWRITE} record_length : shortint; {expected record length} end_of_file : boolean; {declare exit condition} . . . begin 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 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 accessExample 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 } 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 the MPE/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 FREADSEEKIf 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.
|