Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Fortran 90 Programmer's Reference: HP Fortran 90 Programmer's Reference > Chapter 8 I/O and file handling

File access methods

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

HP Fortran 90 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:

OPEN(40, ACCESS="DIRECT", RECL=128, FILE="new_data")

If you do not specify an access method, the file is opened for sequential access.

The following sections describe both sequential and direct methods.

Sequential access

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

  • List-directed I/O

  • Namelist-directed I/O

  • Unformatted I/O

Formatted I/O

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

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:

READ *, A, B, C

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

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

Value

Meaning

z

A null value, indicated by two successive separators with zero or more intervening blanks (for example, ,b/).

c

A literal constant with no embedded blanks. It must be readable by an I, F, A, or L edit descriptor. Binary, octal, and hexadecimal data are illegal.

r*c

Equivalent to r (an integer) successive occurrences of c in the input record. For example, 5*0.0 is equivalent to 0.0 0.0 0.0 0.0 0.0.

r*z

Equivalent to r successive occurrences of z.

 

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

Data type

Input format rules

Integer

Conforms to the same rules as integer constants.

Real and double precision

Any valid form for real and double precision. In addition, the exponent can be indicated by a signed integer constant (the Q, D, or E can be omitted), and the decimal point can be omitted for those values with no fractional part.

Complex and double complex

Two integer, real, or double precision constants, separated by a comma and enclosed in parentheses. The first number is the real part of the complex or double complex number, and the second number is the imaginary part. Each of the numbers can be preceded or followed by blanks or the end of a record.

Logical

Consists of a field of characters, the first nonblank character of which must be a T for true or an F for false (excluding the optional leading decimal point). Integer constants may also appear.

Character

Same form as character constants. Delimiting with single or double quotation marks is needed only if the constant contains any separators; delimiters are discarded upon input. Character constants can be continued from one record to the next. The end-of-record does not cause a blank or any other character to become part of the constant. If the length of the character constant is greater than or equal to the length, len, of the list item, only the leftmost len characters of the constant are transferred. If the length of the constant is less than len, the constant is left-justified in the list item with trailing blanks.

 

Output

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 90 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

Data type

Output format rules

Integer

Output as an integer constant.

Real and Double Precision

Output with or without an exponent, depending on the magnitude. Also, output with field width and decimal places appropriate to maintain the precision of the data as closely as possible.

Complex

Output as two numeric values separated by commas and enclosed in parentheses.

Logical

If the value of the list element is .TRUE., then T is output. Otherwise, F is output.

Character

Output using the Alen format descriptor, where len is the length of the character expression (adjusted for doubling). If DELIM='NONE' (the default), no single (") or double (") quotation marks are doubled, and the records may not be suitable list-directed input. If the value specified by DELIM= is not 'NONE', only the specified delimiter is doubled. Character strings are output without delimiters, making them also unsuitable for list-directed input.

 

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:

PRINT *, "Hello, world!"

outputs the line (where b indicates a blank):

bHello,bworld!

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

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 90 allows this use of the PRINT statement in namelist I/O.)

INTEGER :: i, j
CHARACTER(LEN=10) :: c
NAMELIST /name_group/ i, j, c
...
READ (UNIT=27,NML=name_group)
PRINT name_group

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:

PROGRAM namelist
INTEGER, DIMENSION(4) :: ivar
CHARACTER(LEN=3), DIMENSION(3,2) :: cvar
LOGICAL :: lvar
REAL :: rvar
NAMELIST /nl/ ivar, cvar, lvar, rvar
READ (*,nl)
PRINT nl
END PROGRAM namelist

If the input data is:

&nl
ivar = 4,3,2,1
lvar=toodles
cvar=,,"QRS",2*,2*"XXX"
rvar=5.75E25, cvar(3,2)(1:2)="AB"
/

then the output will be:

b&NLbIVAR   = 4 3 2 1bCVAR   ="", "QRS", "", 
"", "XXX", "ABX"bLVAR = TbRVAR =
5.75000E+25b/

The following sections describe the format of namelist-directed input and output. See “NAMELIST” for detailed information about the NAMELIST statement.

Input

A namelist-directed input record takes the following form:

  1. An ampersand character (&) immediately followed by a namelist group name. The group name must have been previously defined by a NAMELIST statement.

    As an extension, the dollar sign ($) can be substituted for the ampersand.

  2. A sequence of name-value pairs and value separators. A name-value pair consists of the name of a variable in the namelist group, the equals sign (=), and a value having the same format as for list-directed input (z, c, r*c, and r*). A name-value pair can appear in any order in the sequence or can be omitted.

    A value separator may be one of the following:

    • Blanks

    • Tabs

    • Newlines

    • Any of the above with a single comma

  3. A terminating slash (/). As an extension, ($END) can be substituted for the slash.

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”.

Output

The output record for namelist-directed I/O has the same form as the input record, but with these exceptions:

  • The namelist group name is always in uppercase.

  • Logical values are either T or F.

  • As in list-directed output, character values are output without delimiters by default, making them unsuitable for namelist-directed input. However, you can use the DELIM= specifier in the OPEN statement to specify the single or double quotation mark as the delimiter to use for character constants.

  • Only character and complex values may be split between two records.

Unformatted I/O

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.

Direct access

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:

  • The READ statement inputs from the current record, and the file pointer moves to the next record.

  • The WRITE statement outputs to the record at the position of the file pointer, and the file pointer is advanced to the next record.

As an extension, HP Fortran 90 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”.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.