XL Fortran for AIX 8.1

Language Reference


Units

A unit is a means of referring to an external file. Programs refer to external files by the unit numbers indicated by unit specifiers in input/output statements. See [UNIT=] for the form of a unit specifier.

Connection of a Unit

The association of a unit with an external file is called a connection. A connection must occur before the records of the file can be read or written.

There are three ways to connect a file to a unit:

Preconnection

Preconnection occurs once the program begins executing. Preconnected units can be specified in input/output statements without the prior execution of an OPEN statement.

+-------------------------------IBM Extension--------------------------------+

Units 0, 5, and 6 are preconnected to unnamed files for formatted sequential access:

The other properties for these files are the default specifier values for OPEN specifiers, except the following:

+----------------------------End of IBM Extension----------------------------+

+-------------------------------IBM Extension--------------------------------+

Implicit Connection

A file with a predetermined name becomes implicitly connected to a unit (by default, unit n to a file named fort.n) when a sequential PRINT, READ, WRITE, REWIND, or ENDFILE statement is executed on a unit that is not currently connected to an external file. Only unit 0 cannot be implicitly connected. These files need not exist and are created if you use their units without first performing an OPEN statement. The default connection is for sequential input/output. (To implicitly connect to a different file name, see the UNIT_VARS run-time option under "Setting Runtime Options for Input/Output" in the User's Guide.)

A preconnected unit can only be implicitly connected if the connection of the unit to the external file was terminated. In the next example, the preconnected unit is closed before implicit connection can take place:

      PROGRAM TRYME
      WRITE ( 6, 10 ) "Hello1"   ! "Hello1" written to standard output
      CLOSE ( 6 )
      WRITE ( 6, 10 ) "Hello2"   ! "Hello2" written to fort.6
10    FORMAT (A)
      END

The properties of an implicitly connected unit are the default specifier values for the OPEN statement, except for the FORM= and ASYNCH= specifiers, whose value is determined by the first data transfer statement. The value of the FORM= specifier is set to FORMATTED when the first input/output statement uses format-directed, list-directed or namelist formatting; and is set to UNFORMATTED when the first input/output statement is unformatted. The value of the ASYNCH= specifier is set to YES when the first input/output statement is asynchronous; and is set to NO when the first input/output statement is synchronous.

+----------------------------End of IBM Extension----------------------------+

Disconnection

The CLOSE statement disconnects a file from a unit. The file can be connected again within the same program to the same unit or to a different unit, and the unit can be connected again within the same program to the same file or a different file.

+-------------------------------IBM Extension--------------------------------+

Unit 0 cannot be closed. Units 5 and 6 cannot be reconnected to standard input and standard output, respectively, after having been closed.

+----------------------------End of IBM Extension----------------------------+

Executing Data Transfer Statements

The READ statement obtains data from an external or internal file and places it in internal storage. Values are transferred from the file to the data items specified by the input list, if one is specified.

The WRITE statement places data obtained from internal storage into an external or internal file. The PRINT statement places data obtained from internal storage into an external file. Values are transferred to the file from the data items specified by the output list and format specification, if you specify them. Execution of a WRITE or PRINT statement for a file that does not exist creates the file, unless an error occurs.

If the output list is omitted in a PRINT statement, a blank record is transmitted to the output device unless the FORMAT statement referred to contains as its first specification a character string edit descriptor or a slash edit descriptor. In this case, the records indicated by these specifications are transmitted to the output device.

Zero-sized arrays and implied-DO lists with iteration counts of zero are ignored when determining the next item to be processed. Zero-length scalar character items are not ignored.

If an input/output item is a pointer, data is transferred between the file and the associated target.

During advancing input from a file whose PAD= specifier has the value NO, the input list and format specification must not require more characters from the record than the record contains. If the PAD= specifier has the value YES or if the input file is an internal file, blank characters are supplied if the input list and format specification require more characters from the record than the record contains.

+-------------------------------IBM Extension--------------------------------+

If you want only external files connected for sequential access to be padded, specify the noblankpad suboption of the -qxlf77 compiler option, which also sets the default value for the PAD= specifier to NO for direct files and YES for sequential files.

+----------------------------End of IBM Extension----------------------------+

During nonadvancing input from a file whose PAD= specifier has the value NO, an end-of-record condition occurs if the input list and format specification require more characters from the record than the record contains. If the PAD= specifier has the value YES, an end-of-record condition occurs and blank characters are supplied if an input item and its corresponding data edit descriptor require more characters from the record than the record contains.

+-------------------------------IBM Extension--------------------------------+

Executing Data Transfer Statements Asynchronously

Synchronous Input/Output (I/O) halts the execution of an application until the I/O operation is complete. Asynchronous I/O allows an application to continue processing while the I/O operation is performed in the background.

Fortran asynchronous READ and WRITE data transfer statements are used to initiate asynchronous data transfer. Execution continues after the asynchronous I/O statement whether or not the actual data transfer is complete. The execution of the data transfer statement must eventually be followed by the execution of a matching WAIT statement specifying the same ID= value that was returned to the ID= variable in the data transfer statement. See WAIT for the definition of the matching WAIT statement.

The actual data transfer of an I/O item specified in an asynchronous I/O statement may be completed:

There are, however, situations where the actual data transfer must be completed during the asynchronous data transfer statement. For more information on these types of situations, see "AIX Implementation Details of XL Fortran Input/Output" in the User's Guide.

If an error occurs during the data transfer statement, the matching WAIT statement will not be required because the ID= value will not be defined. Otherwise, error handling and status reporting (ERR= and IOSTAT=) are performed as if the data transfer statement had been executed synchronously in place of the matching WAIT statement.

Any variable that appears as an I/O list item in an asynchronous data transfer statement, or that is associated with such a variable, must not be referenced, become defined, or become undefined until the execution of the matching WAIT statement.

Any deallocation of allocatable objects and pointers and changing association status of pointers are also disallowed between an asynchronous data transfer statement and the matching WAIT statement.

Multiple outstanding asynchronous data transfer operations on the same unit are allowed, but they must all be READs or all be WRITEs. No other I/O statements on the same unit are allowed until the matching WAIT statements for all outstanding asynchronous data transfer operations on the same unit are executed. In the case of direct access, an asynchronous WRITE statement must not specify both the same unit and record number as any asynchronous WRITE statement for which the matching WAIT statement has not been executed.

In the portion of the program that executes between the asynchronous data transfer statement and the matching WAIT statement, the integer_variable in the NUM= specifier or any variable associated with it must not be referenced, become defined, or become undefined.

Example:

! A program demonstrating the use of asynchronous I/O statements.
 
SUBROUTINE COMPARE(ISTART, IEND, ISIZE, A)
INTEGER, DIMENSION(ISIZE) :: A
INTEGER I, ISTART, IEND, ISIZE
DO I = ISTART, IEND
  IF (A (I) /= I) THEN
    PRINT *, "Expected ", I, ", got ", A(I)
  END IF
END DO
END SUBROUTINE COMPARE
 
PROGRAM SAMPLE
INTEGER, PARAMETER :: ISIZE = 1000000
INTEGER, PARAMETER :: SECT1 = (ISIZE/2) - 1, SECT2 = ISIZE - 1
INTEGER, DIMENSION(ISIZE), STATIC :: A
INTEGER IDVAR
 
OPEN(10, STATUS="OLD", ACCESS="DIRECT", ASYNCH="YES", RECL=(ISIZE/2)*4)
A = 0
 
! Reads in the first part of the array.
 
READ(10, REC=1) A(1:SECT1)
 
! Starts asynchronous read of the second part of the array.
 
READ(10,ID=IDVAR, REC=2) A(SECT1+1:SECT2)
 
! While the second asynchronous read is being performed,
! do some processing here.
 
CALL COMPARE(1, SECT1, ISIZE, A)
 
WAIT(ID=IDVAR)
 
CALL COMPARE(SECT1+1, SECT2, ISIZE, A)
END

Related Information:

+----------------------------End of IBM Extension----------------------------+

Advancing and Nonadvancing Input/Output

Advancing input/output positions the file after the last record that is read or written, unless an error condition is encountered.

Nonadvancing input/output can position the file at a character position within the current record, or a subsequent record. With nonadvancing input/output, you can read or write a record of the file by a sequence of input/output statements that each access a portion of the record. You can also read variable-length records and inquire about their lengths.

! Reads digits using nonadvancing input
 
    INTEGER COUNT
    CHARACTER(1) DIGIT
    OPEN (7)
    DO
     READ (7,FMT="(A1)",ADVANCE="NO",EOR=100) DIGIT
       COUNT = COUNT + 1
     IF ((ICHAR(DIGIT).LT.ICHAR('0')).OR.(ICHAR(DIGIT).GT.ICHAR('9'))) THEN
       PRINT *,"Invalid character ", DIGIT, " at record position ",COUNT
       STOP
     END IF
    END DO
100 PRINT *,"Number of digits in record = ", COUNT
    END

File Position Before and After Data Transfer

For an explicit connection (by an OPEN statement) for sequential input/output that specifies the POSITION= specifier, the file position can be explicitly positioned at the beginning, at the end, or where the position is on opening.

If the OPEN statement does not specify the POSITION= specifier:

+-------------------------------IBM Extension--------------------------------+

After an implicit OPEN, the file is positioned at the beginning. Thus:

+----------------------------End of IBM Extension----------------------------+

A REWIND statement can be used to position a file at its beginning. The preconnected units 0, 5 and 6 are positioned as they come from the program's parent process.

The positioning of a file prior to data transfer depends on the method of access:

After advancing input/output data transfer, the file is positioned:

For nonadvancing input, if no error condition or end-of-file condition occurs, but an end-of-record condition occurs, the file is positioned just after the record read. If no error condition, end-of-file condition or end-of-record condition occurs in a nonadvancing input statement, the file position does not change. If no error condition occurs in a nonadvancing output statement, the file position is not changed. In all other cases, the file is positioned just after the record read or written and that record becomes the preceding record.

If a file is positioned beyond the endfile record, a READ, WRITE, PRINT, or ENDFILE statement cannot be executed (assuming -qxlf77=softeof is not set). A BACKSPACE or REWIND statement can be used to reposition the file.

+-------------------------------IBM Extension--------------------------------+

Use the -qxlf77=softeof option to be able to read and write past the end-of-file. See "-qxlf77 Option" in the User's Guide for details.

+----------------------------End of IBM Extension----------------------------+


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]