| United States-English |
|
|
|
![]() |
HP Fortran Compiler for HP-UX: HP Fortran Programmer's Reference > Chapter 8 I/O
and file handlingFile access methods |
|
HP Fortran allows both sequential access and direct access. You specify the access method with the OPEN statement when you connect the file to a unit number. The following example opens the file new_data for direct access:
If you do not specify an access method, the file is opened for sequential access. The following sections describe both sequential and direct methods. Records in a file opened for sequential access can be accessed only in the order in which they were written to the file. A sequential file may consist of either formatted or unformatted records. If the records are formatted, you can use list-directed, namelist-directed, and formatted I/O statements to operate on them. If the records are unformatted, you must use unformatted I/O statements only. The last record of a sequential file is the end-of-file record. The following sections describe the types of I/O that can be used with sequential files, namely:
Formatted I/O uses format specifications to define the appearance of data input to or output from the program, producing ASCII records that are formatted for display. (Format specifications are described in detail in “Format specification”.) Data is transferred and converted, as necessary, between binary values and character format. You cannot perform formatted I/O on a file that has been connected for unformatted I/O; see “Unformatted I/O”. Formatted I/O can be performed only by data transfer statements that include a format specification. The format specification can be defined in the statement itself or in a FORMAT statement referenced by the statement. For an example of a program that accesses a formatted file, see “File access”. List-directed I/O is similar to formatted I/O in that data undergoes a format conversion when it is transferred but without the use of a format specification to control formatting. Instead, data is formatted according to its data type. List-directed I/O is typically used when reading from standard input and writing to standard output. List-directed I/O uses the asterisk (*) as a format identifier instead of a list of edit descriptors, as in the following READ statement, which reads three floating-point values from standard input:
List-directed I/O can be performed only on internal files and on formatted, sequential external files. It works identically for both file types. Input data for list-directed input consists of values separated by one or more blanks, a slash, or a comma preceded or followed by any number of blanks. (No values may follow the slash.) An end-of-record also acts as a separator except within a character constant. Leading blanks in the first record read are not considered to be part of a value separator unless followed by a slash or comma. Input values can be any of the values listed in Table 8-1 “Input values for list-directed I/O”. A blank is indicated by the symbol b. Table 8-1 Input values for list-directed I/O
Reading always starts at the beginning of a new record. Records are read until the list is satisfied, unless a slash in the input record is encountered. The effect of the slash is to terminate the READ statement after the assignment of the previous value; any remaining data in the current record is ignored. Table 8-2 “Format of list-directed input data” outlines the rules for the format of list-directed input data. Table 8-2 Format of list-directed input data
The format of list-directed output is determined by the type and value of the data in the output list and by the value of the DELIM= specifier in the OPEN statement. For information about the DELIM= specifier, see the description of the OPEN statement in Chapter 10 “HP Fortran statements”. Table 8-3 “Format of list-directed output data” summarizes the rules governing the display of each data type. Table 8-3 Format of list-directed output data
With the exception of character values, all output values are preceded by exactly one blank. A blank character is also inserted at the start of each record to provide ASA carriage control if the file is to be printed; see “ASA carriage control” for a description of this. For example, the following statement:
outputs the line (where b indicates a blank):
If the length of the values of the output items is greater than 79 characters, the current record is written and a new record started. Slashes, as value separators, and null values are not output by list-directed WRITE statements. Namelist-directed I/O enables you to transfer a group of variables by referencing the name of the group, using the NML= specifier in the data transfer statement. The NAMELIST statement specifies the variables in the group and gives the group a name. Like list-directed I/O, namelist-directed I/O does not use a format specification when formatting data but uses default formats, as determined by the data types. In the following example, the NAMELIST statement defines the group name_group, which consists of the variables i, j, and c. The READ statement reads a record from the file connected to unit number 27 into name_group. The PRINT statement then writes the data from the variables in name_group to standard output. (As an extension, HP Fortran allows this use of the PRINT statement in namelist I/O.)
Each namelist-directed output record begins with a blank character to provide for ASA carriage control if the records are to be printed (see “ASA carriage control”). Namelist-directed I/O can be performed only on formatted, sequential external files. The following program illustrates namelist-directed I/O:
If the input data is:
then the output will be:
The following sections describe the format of namelist-directed input and output. See “NAMELIST” for detailed information about the NAMELIST statement. A namelist-directed input record takes the following form:
Names of character type may be qualified by substring range expressions and array names by subscript/array section expressions. If the name in a name-value pair is that of an array, the number of the values following the equals sign must be separated by value separators and must not exceed the number of elements in the array. If there are fewer values than elements, null values are supplied for the unfilled elements. Namelist-directed input values are formatted according to the same rules as for list-directed input data; see Table 8-2 “Format of list-directed input data”. The output record for namelist-directed I/O has the same form as the input record, but with these exceptions:
Unformatted I/O does not perform format conversion on data it transfers. Instead, data is kept in its internal, machine-representable format. You cannot perform unformatted I/O on files that have been connected for formatted I/O (see “Formatted I/O”). Unformatted I/O is more efficient than formatted, list-directed, or namelist-directed I/O because the transfer occurs without the conversion overhead. However, because unformatted I/O transfers data in internal format, it is not portable. When performing I/O on a direct-access file, records can be read or written in any order. The records in a direct-access file are all of the same length. Reading and writing records is accomplished by READ and WRITE statements containing the REC= specifier. Each record is identified by a record number that is a positive integer. For example, the first record is record number 1; the second, number 2; and so on. If REC= is not specified:
As an extension, HP Fortran allows sequential I/O statements to access a file connected for direct access. Once established, a record number of a specific record cannot be changed or deleted, although the record may be rewritten. A direct-access file does not contain an end-of-file record as an integral part of the file with a specific record number. Therefore, when accessing a file with a direct-access read or write statement, the END= specifier is not valid and is not allowed. Direct-access files support both formatted and unformatted record types. Both formatted and unformatted I/O work exactly as they do for sequential files. However, you cannot perform list-directed, namelist-directed, or nonadvancing I/O on direct-access files. For an example program that uses direct access, see “File access”. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||