|
|
Accessing Files Programmer's Guide: HP 3000 MPE/iX Computer Systems > Chapter 9 Reading from a FileSequential Access and Random Access |
|
Two of the most frequently used methods of transferring data from a file to your program are sequential access and random access. When you use sequential access to read data from a file, you read data from the record currently pointed to by the record pointer. You use the FREAD intrinsic to read data sequentially from a disk file or device file. When you open a file with any form of Read access specified in the access type option of HPFOPEN/FOPEN, the file is opened with the record pointer set to the first record in the file. When you have accomplished the read operation, the file system automatically sets the record pointer to point to the beginning of the next record in the file. Both disk files and device files can be accessed with the FREAD intrinsic. When you use random access to read data from a disk file, you read data from any record in the file by specifying where you want the file system to set the record pointer prior to the read operation. You use the FREADDIR intrinsic to randomly access records in a disk file. You must specify in FREADDIR which record that you want to read from. The file system sets the record pointer to the selected record, then transfers the data from the record to your program's stack. When you have accomplished the read operation, the file system automatically sets the record pointer to point to the beginning of the next record in the file. Only disk files can be accessed with the FREADDIR intrinsic The following examples illustrate the use of file system intrinsics to perform sequential access reads and random access reads from a disk file. Example 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
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 (32650-90028). 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". 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
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 (32650-90028). 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". 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. |
|