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 Compiler for HP-UX: HP Fortran Programmer's Reference > Chapter 8 I/O and file handling

Syntax of I/O statements

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

The general syntactic form of file-positioning and auxiliary statements is:

statement-name (io-specifier-list)

where

statement-name

is one of the statements listed in Table 8-5 “File positioning statements” or Table 8-6 “Auxiliary statements”.

io-specifier-list

is a comma-separated list of I/O specifiers that control the statement’s operation.

The general form of a data-transfer statement is:

statement-name (io-specifier-list) data-list

where

statement-name

is one of the statements listed in Table 8-4 “Data transfer statements”.

io-specifier-list

is a comma-separated list of I/O specifiers that control the data transfer.

data-list

is a comma-separated list of data items.

The following sections describe the I/O specifiers and the form of data-list. For detailed information about the syntax of individual I/O statements, see Chapter 10 “HP Fortran statements”.

I/O specifiers

I/O specifiers provide I/O statements with additional information about a file or a data transfer operation. They can also be used (especially with the INQUIRE statement) to return information about a file. Table 8-7 “I/O statements and specifiers” lists all I/O specifiers supported by HP Fortran and identifies the statements in which each can appear. Note that the ACCEPT, DECODE, ENCODE, and TYPE statements are not listed in the table as they are nonstandard. All I/O specifiers and statements are fully described in Chapter 10 “HP Fortran statements”. Each I/O specifier is described under the I/O statement in which it may appear.

Table 8-7 I/O statements and specifiers

I/O SpecifiersBACKSPACECLOSEENDFILEINQUIREOPENPRINTREADREWINDWRITE
ACCESS=       
ACTION=       
ADVANCE=       
BLANK=       
DELIM=       
DIRECT=        
END=        
EOR=        
ERR= 
EXIST=        
FILE=       
FMT=       
FORM=       
FORMATTED=        
IOLENGTH=        
IOSTAT= 
NAME=        
NAMED=        
NEXTREC=        
NML=       
NUMBER=        
OPENED=        
PAD=       
POSITION=       
READ=        
READWRITE=        
REC=       
RECL=       
SEQUENTIAL=        
SIZE=        
STATUS=       
UNFORMATTED=        
UNIT= 
WRITE=        

 

I/O data list

The I/O data list can be used with any data transfer statement except namelist I/O; see “Namelist-directed I/O” for a description of this. The general form of the I/O data list is:

item1[, item2...]

where item is a either a simple data element or an implied-DO loop.

The following sections describe simple data elements and the implied-DO loop.

Simple data elements

In a read operation, the simple data element specifies a variable, which can include:

  • A scalar

  • An array

  • An array element or section

  • A character substring

  • A structure

  • A component of a structure

  • A record

  • A field of a record

  • A pointer

In a write operation, the simple data element can include any variable that is valid for a read operation, plus most expressions. Note that, if the expression includes a function reference, the function must not itself perform I/O.

The output list in the following PRINT statement contains two simple list elements, a variable named radius and an expression formed from radius:

99 FORMAT('Radius = ', F10.2, 'Area = ', F10.2)
PRINT 99, radius, 3.14159*radius**2

The next READ statement contains three simple elements: a character substring (name(1:10)), a variable (id), and an array name (scores):

88 FORMAT(A10,I9,10I5)
READ(5, 88) name(1:10), id, scores

If an array name is used as a simple data element in the I/O list of a WRITE statement, then every element in the array will be displayed. If a format specification is also used, then the format will be reused if necessary to display every element. For example, the following code

   INTEGER :: i(10) = (/1,2,3,4,5,6,7,8,9,10/)
88 FORMAT(' N1:',I5, ' N2:',I5, ' N3:',I5)
PRINT 88, i

will output the following:

   N1:    1 N2:    2 N3:    3
N1: 4 N2: 5 N3: 6
N1: 7 N2: 8 N3: 9
N1: 10 N2:

The following restrictions apply to the use of arrays in input and output:

  • Sections of character arrays that specify vector-valued subscripts cannot be used as internal files.

  • An assumed-size array cannot be referenced as a whole array in an input or output list.

The following restrictions apply to the use of structures and records in input and output:

  • All components of the structure or fields of the record must be accessible within the scoping unit that contains the data transfer statement.

  • Every component of the structure or field of the record is written.

  • A structure in an I/O list must not contain a pointer that is an ultimate component—that is, the last component in a variable reference. In the expression a%b%c, a and b can be pointers, but not c.

Implied-DO loop

An implied-DO loop consists of a list of data elements to be read, written, or initialized, and a set of indexing parameters. The syntax of an implied-DO loop in an I/O statement is:

(list , index = init , limit [, step ])

where

list

is an I/O list, which can contain other implied-DO loops.

index

is an integer variable that controls the number of times the elements in list are read or written. The use of real variables is supported but obsolescent.

init

is an expression that is the initial value assigned to index at the start of the implied-DO loop.

limit

is an expression that is the termination value for index.

step

is an expression by which index is incremented or decremented after each execution of the DO loop. step can be positive or negative. Its default value is 1.

Inner loops can use the indexes of outer loops.

The implied-DO loop acts like a DO construct. The range of the implied-DO loop is the list of elements to be input or output. The implied-DO loop can transfer a list of data elements that are valid for a write operation. index is assigned the value of init at the start of the loop. Execution continues in the same manner as for DO loops (see DO construct”).

The implied-DO loop is generally used to transmit arrays and array elements, as in the following:

INTEGER :: b(10)
PRINT *, (b(i), i = 1,10)

If b has been initialized with the values 1 through 10 in order, the PRINT statement will produce the following output:

1 2 3 4 5 6 7 8 9 10

If an nonsubscripted array name occurs in the list, the entire array is transmitted at each iteration. For example:

REAL :: x(3)
PRINT *, (x, i=1, 2)

If x has been initialized to be [ 1 2 3 ], the output will be:

 1.0 2.0 3.0 1.0 2.0 3.0

The list can contain expressions that use the index value. For example:

REAL :: x(10) = (/.1, .2, .3, .4, .5, .6,  .7, .8, .9, 1 /)
PRINT *, (i*2, x(i*2), i = 1, 5)

print the numbers

2 .2 4 .4 6 .6 8 .8 10 1

Implied-DO loops can also be nested. The form of a nested implied-DO loop in an I/O statement is:

(((list, index1 = init1, limit1, step1), index2 = init2, limit2,
step2) ... indexN = initN, limitN, stepN)

Nested implied-DO loops follow the same rules as do other nested DO loops. For example, given the following statements:

REAL :: a(2,2)

a(1,1) = 1
a(2,1) = 2
a(1,2) = 3
a(2,2) = 4

WRITE(6,*)((a(i,j),i=1,2),j=1,2)

the output will be:

 1.0 2.0 3.0 4.0

The first, or nested DO loop, is completed once for each execution of the outer loop.

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