| United States-English |
|
|
|
![]() |
HP Fortran 90 Programmer's Reference: HP Fortran 90 Programmer's Reference > Chapter 10 HP Fortran 90 statementsOPEN |
|
Connects file to a unit. Syntax
DescriptionThe OPEN statement connects a unit to a file so that data can be read from or written to that file. Once a file is connected to a unit, the unit can be referenced by any program unit in the program. I/O specifiers do not have to appear in any specific order in the OPEN statement. However, if the optional keyword UNIT= is omitted, unit must be the first item in the list. Only one unit can be connected to a file at a time. That is, the same file cannot be connected to two different units. Attempting to open a file that is connected to a different unit will produce undefined results. However, multiple OPENs can be performed on the same unit. In other words, if a unit is connected to a file that exists, it is permissible to execute another OPEN statement for the same unit. If FILE= specifies a different file, the previously opened file is automatically closed before the second file is connected to the unit. If FILE= specifies the same file, the file remains connected in the same position; the values of the BLANK=, DELIM=, PAD=, ERR=, and IOSTAT= specifiers can be changed, but attempts to change the values of any of the other specifiers will be ignored. ExamplesThe following examples illustrate different uses of the OPEN statement. Opening a file for sequential access The following OPEN statement connects the existing file inv to unit 10 and opens it (by default) for sequential access. Only READ statements are permitted to perform data transfers. If an error occurs, control passes to the executable statement labeled 100 and the error code is placed in the variable ios:
Opening a file for direct access The following OPEN statement opens the file whose name is contained in the variable next1, connecting it to unit 4 as a formatted, direct-access file with a record length of 50 characters:
Opening a device for I/O transfers The next example connects the system device /dev/console to unit 6; all data transfers that specify unit 6 will go to this device:
Opening a scratch file The following two OPEN statements produce the same results: open a scratch file that is connected to unit 19 (if the FILE=namespecifier had appeared in the first statement, the named file would have been opened instead):
I/O specifiers in an OPEN statement Because the I/O specifiers that can be used in an OPEN statement do not have to appear in any specific order, the following three OPEN statements are all equivalent:
Note, however, that in the second OPEN statement the number 3 must appear first because of the omission of the optional keyword UNIT=. Thus, the following OPEN statement is illegal:
Related statementsCLOSE, INQUIRE, READ, and WRITE Related conceptsFor information about I/O concepts and examples of programs that perform I/O, see Chapter 8 “I/O and file handling”. For information about I/O formatting, see Chapter 9 “I/O formatting”. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||