HP 3000 Manuals

OPEN Statement (Executable) [ HP FORTRAN 77/iX Reference ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Reference

OPEN Statement (Executable) 

The OPEN statement establishes a connection between a unit number and a
file.  It also establishes or verifies the properties of a file.

[]
------------------------------------------------------------------------------------------------ | | | | | Item | Description/Default | Restrictions | | | | | ------------------------------------------------------------------------------------------------ | | | | | unit | Specifies unit number. | Integer expression >= 0. | | | | | ------------------------------------------------------------------------------------------------ | | | | | name | Character variable. | May be fixed or variable. | | | | | ------------------------------------------------------------------------------------------------ | | | | | label | Control transfers to specified executable | Must be the statement label of | | | statement if error encountered on OPEN. | an executable statement in the | | | | same program unit. | | | | | ------------------------------------------------------------------------------------------------ | | | | | ios | ios = 0 if no error; ios = positive value | Integer variable, array element, | | | if error condition exists. | or scalar record field. | | | | | ------------------------------------------------------------------------------------------------ | | | | | sta | Specifies file as 'OLD', 'NEW', 'SCRATCH', | Character variable, array | | | or 'UNKNOWN' (default). See Note 1. | element, substring, or scalar | | | | record field. | | | | | ------------------------------------------------------------------------------------------------ | | | | | acc | Specifies file access to be 'DIRECT', | Character variable, array | | | 'KEYED', or 'SEQUENTIAL' (default). See | element, substring, or scalar | | | Note 2. | record field. | | | | | ------------------------------------------------------------------------------------------------ | | | | | fm | Specifies data format to be 'FORMATTED' or | Character variable, array | | | 'UNFORMATTED'. If absent and | element, substring, or scalar | | | ACCESS='SEQUENTIAL' is specified, | record field. | | | 'FORMATTED' is assumed; If absent and | | | | ACCESS='DIRECT' is specified, 'UNFORMATTED' | | | | is assumed. If absent and KEYED is | | | | specified, UNFORMATTED is assumed. | | | | | | ------------------------------------------------------------------------------------------------ | | | | | rcl | Specifies record length for direct access | Numeric expression. | | | and ISAM files; length is measured in | | | | bytes. | | | | | | ------------------------------------------------------------------------------------------------ | | | | | blnk | Specifies treatment of blanks within | Character variable, array | | | numbers in input. If 'NULL' (default), | element, substring, or scalar | | | blanks are ignored. If 'ZERO', blanks are | record field. | | | treated as zeros. | | | | | | ------------------------------------------------------------------------------------------------ | | | | | mrec | See "Semantics". | Integer variable, array element, | | | | or scalar record field. | | | | | ------------------------------------------------------------------------------------------------ | | | | | use | See "Semantics". | Character variable, array | | | | element, substring, or scalar | | | | record field. | | | | | ------------------------------------------------------------------------------------------------ | | | | | node | See "Semantics". | Integer variable, array element, | | | | or scalar record field. | | | | | ------------------------------------------------------------------------------------------------ | | | | | key_spec | See "Semantics". | Integer variable, character | | | | expression, or scalar record | | | | field. | | | | | ------------------------------------------------------------------------------------------------ | | | | | asvar | See "Semantics". | Value not checked. | | | | | ------------------------------------------------------------------------------------------------ | | | | | blsz | See "Semantics". | Value not checked. | | | | | ------------------------------------------------------------------------------------------------ | | | | | bufct | See "Semantics". | Value not checked. | | | | | ------------------------------------------------------------------------------------------------ | | | | | cctl | See "Semantics". | Value not checked. | | | | | ------------------------------------------------------------------------------------------------ | | | | | dfile | See "Semantics". | Value not checked. | | | | | ------------------------------------------------------------------------------------------------ | | | | | dsp | See "Semantics". | Value not checked. | | | | | ------------------------------------------------------------------------------------------------ | | | | | dsps | See "Semantics". | Value not checked. | | | | | ------------------------------------------------------------------------------------------------ Table 3-8. (cont.) ------------------------------------------------------------------------------------------------ | | | | | Item | Description/Default | Restrictions | | | | | ------------------------------------------------------------------------------------------------ | | | | | exdsz | See "Semantics". | Value not checked. | | | | | ------------------------------------------------------------------------------------------------ | | | | | init | See "Semantics". | Value not checked. | | | | | ------------------------------------------------------------------------------------------------ | | | | | org | See "Semantics". | Value not checked. | | | | | ------------------------------------------------------------------------------------------------ | | | | | uopen | See "Semantics". | Value not checked. | | | | | ------------------------------------------------------------------------------------------------ --------------------------------------------------------------------------------------- | | | Note 1 | | | --------------------------------------------------------------------------------------- | | | | | | If | = | then the | and | | | | | | --------------------------------------------------------------------------------------- | | | | | | STATUS | 'OLD' | FILE specifier is required | The file must exist. | | | | | | | | 'NEW' | FILE specifier is required | The file named must not | | | | | exist. | | | | | | | | 'SCRATCH' | FILE specifier must not be | A scratch file is created. | | | | present | | | | | | | --------------------------------------------------------------------------------------- | | | | | | If | = | and if the | then | | | | | | --------------------------------------------------------------------------------------- | | | | | | STATUS | 'UNKNOWN' | FILE specifier is present | The file named is created if | | | | | it does not already exist. | | | | | | | | | FILE specifier is not | A nondisk unit is connected | | | | present | to the unit specified. | | | | | | --------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------- | | | Note 2 | | | --------------------------------------------------------------------------------------- | | | | | | If | = | then the | and the | | | | | | --------------------------------------------------------------------------------------- | | | | | | ACCESS | 'SEQUENTIAL' | RECL specifier may be | File is opened for | | | | present | sequential access. | | | | | | | | 'DIRECT' | RECL specifier is required | File is opened for direct | | | | | access. | | | | | | | | 'KEYED' | RECL specifier is required. | File is opened as a fixed | | | | RECORDTYPE must be fixed. | length ISAM file. | | | | | | | | 'APPEND' | File is opened for | If ACCESS=`APPEND' is | | | | sequential access beginning | specified with READONLY, a | | | | after the last record of the | runtime error will occur. | | | | file. | | | | | | | --------------------------------------------------------------------------------------- Semantics The name field can also be the ASCII representation of a device file. The UNIT specifier is required in the keyword list. If the prefix UNIT= is omitted, unit must be the first item in the list. At most one each of the other items can appear in the keyword list. If the ERR specifier is present and an error occurs during execution of the OPEN statement, control transfers to the specified statement rather than aborting the program. If the IOSTAT specifier is present and an error occurs, the error code is returned in the ios variable and the program is not aborted. Refer to Appendix A for IOSTAT error codes. For the character expressions used with STATUS, ACCESS, FORM, and BLANK, only the first character in each is significant. The following specifiers, extensions to the ANSI 77 standard, are included for compatibility with programs originally written in another version of FORTRAN. ASSOCIATEVARIABLE DISPOSE NOSPANBLOCKS BLOCKSIZE EXTENDSIZE ORGANIZATION BUFFERCOUNT INITIALSIZE RECORDSIZE CARRIAGECONTROL MAXREC TYPE DEFAULTFILE NAME USE DISP NODE USEROPEN If used in a program, their syntax is checked, but they are otherwise ignored by the compiler. Once a file is connected to a unit number, the unit can be referenced by any program unit in the program. If a unit is already connected to an existing file, execution of another OPEN statement for that unit is permitted. If the FILE specifier is absent or the file name is the same, the current file remains connected. Otherwise, an automatic close is performed before the new file is connected to the unit. A redundant OPEN call can be used to change only the value of the BLANK option. However, attempts to change the values of any other specifiers with a redundant OPEN are ignored. A redundant OPEN does not affect the current position of the file. The same file cannot be connected to two different units. An attempt to open a file that is connected to a different unit by the same name causes an error. As an extension to the ANSI 77 standard, indexed sequential access (ISAM) is allowed on an OPEN statement. Indexed files can be accessed with a key, which is part of the record. The specifier KEY=key_spec specifies the length of the key. key_spec has the form: exp1 : exp2 [: data_type] where exp1 is the first byte position of the key and exp2 is the last byte position of the key. data_type is the data type of the key and must be integer or character. The length of the key is determined by the expression: exp2 - exp1 + 1 The following table shows the use of ACCESS, RECL, and RECORDTYPE to determine whether an indexed file is variable or fixed length. -------------------------------------------------------------------------------------------------- | | | | | | ACCESS | RECL | RECORDTYPE | File Type | | | | | | -------------------------------------------------------------------------------------------------- | | | | | | 'SEQUENTIAL' | Absent | | Variable length file | | | | | | -------------------------------------------------------------------------------------------------- | | | | | | 'SEQUENTIAL' | Present | | Variable length file | | | | | (RECL = maximum record length) | | | | | | -------------------------------------------------------------------------------------------------- | | | | | | 'DIRECT' | Absent | | Error | | | | | | -------------------------------------------------------------------------------------------------- | | | | | | 'DIRECT' | Present | | Fixed length file | | | | | | -------------------------------------------------------------------------------------------------- | | | | | | 'KEYED' | Present | Variable | Error | | | | | (RECL = maximum record length) | | | | | | -------------------------------------------------------------------------------------------------- | | | | | | 'KEYED' | Present | Fixed | Fixed length index sequential access file | | | | | | -------------------------------------------------------------------------------------------------- | | | | | | 'KEYED' | Absent | Variable | Error | | | | | (Maximum record length is 2048 bytes) | | | | | | -------------------------------------------------------------------------------------------------- | | | | | | 'KEYED' | Absent | Fixed | Error | | | | | | -------------------------------------------------------------------------------------------------- By default, files are opened for shared read/write access. Rewinding a file opened with ACCESS=`APPEND' repositions the file pointer at the beginning of the file. Backspacing a file that is opened with ACCESS=`APPEND' can reposition the file pointer beyond the initial access point. An inquire with ACCESS=acc returns acc='SEQUENTIAL' for files opened with ACCESS='APPEND'. If ACCESS=`APPEND' is specified with READONLY, a runtime error will occur. The READONLY specifier causes the file to be opened for read only access. READONLY can be specified on a file to prevent writing into it by accident. Any attempt to write to a read-only file generates a "FILE SYSTEM ERROR" message. The SHARED specifier explicitly sets the file for shared access. This permits the file to be shared by multiple programs. Since shared is also the default condition, SHARED has no effect.
NOTE When a file is opened with the unit specifier specified, but with no file specifier, a scratch file is opened. Therefore, the following two statements are equivalent: OPEN (UNIT=19) OPEN (UNIT=19, STATUS='SCRATCH')
Examples Notes --------------------------------------------------------------------------------------- OPEN (10,FILE='inv', The file inv is connected to unit 10 as a 1ACCESS='SEQUENTIAL', sequential file. If an error occurs, 2ERR=100,IOSTAT=ios) control transfers to statement 100 and the error code is placed in the variable ios. OPEN (ACCESS='DIRECT', The character variable next1 contains the 1UNIT=4,RECL=50, name of the file to be connected to unit 4 2FORM='FORMATTED',FILE=next1) as a formatted, direct access file with a record length of 50 characters. program append character*2 FN character*20 STR, line integer recnum PARAMETER(LU=15) PARAMETER(FN='Afile') PARAMETER(STR='This is record') C Open file and write to it sequentially CALL OPEN_AND_WRITE(LU,FN,STR) C Open existing file for APPEND access OPEN(unit=LU,file=FN,access='append',iostat=ios,err=99) DO I = 26, 50 WRITE(LU,500) STR, i END DO REWIND(LU) DO I = 1, 50 READ(LU,500) line, recnum IF (line .ne. STR) THEN WRITE(6,*) 'line = ',line STOP 'READ FAILED - read back incorrect' END IF IF (recnum .ne. i) THEN WRITE(6,*) 'recnum = ',recnum STOP 'READ FAILED - read back incorrect' END IF END DO CLOSE(LU,status='keep') STOP 99 continue WRITE(6,*) 'iostat value = ',ios STOP 'APPEND OPEN FAILED' 500 FORMAT(2X,A20,I4) END subroutine OPEN_AND_WRITE(LUNIT,FNAME,STR) character*2 FNAME character*20 STR OPEN(unit=LU,file=FNAME,access='sequential',iostat=ios,err=98) DO I = 1, 25 WRITE(LU,499) STR, i END DO CLOSE(LU) 98 continue WRITE(6,*) 'iostat value = ',ios STOP 'OPEN FAILED' 499 FORMAT(2X,A20,I4) END


MPE/iX 5.0 Documentation