The open statement is an executable statement.
The open command assigns an integer number to a needed file for use in program input and output (READ and WRITE). It can make both new and old files available for the program's use.
open (open_arguments)Required Arguments:
This argument assigns the integer value to the file for reference purposes. The value may be provided with either an integer constant or an integer variable. However, this "unit=" specifier is really not needed to assign the value. Entering a single integer value as the first thing after the left parenthesis will serve the same purpose. The only reason why programmers will use the UNIT specifier is to increase the clarity of the OPEN statement.
The name of the file to be referenced is placed in quotation marks after the equals sign.
There are five different character strings that can be placed in quotation marks after the equals sign. The first of the character strings is 'NEW' which is used when you create a new file. Next, is the 'OLD' statement. Naturally, this will be used when referencing a pre-existing file. Third is the 'SCRATCH' statement. This type of file has a life span that is limited to the duration of the program as it is automatically deleted when the program terminates. The fourth character string is the 'REPLACE' statement. This is new to Fortran 90 and it will cause the compiler to erase all information contained within the file before it is accessed. Finally is the 'UNKNOWN' specifier. This is Fortran's default condition when the status argument is not present. When 'UNKNOWN' is used, the compiler will create a new file if needed or link to a pre-existing file if needed.
There are only two arguments that can be placed here. They are 'FORMATTED' and 'UNFORMATTED'. By default, most compilers will assume a file to be formatted.
Specifies an INTEGER variable that will contain an error condition number. A value of zero means no error. A negative value indicates that the End of File or End of Record (you tried to read more numbers than existed in the record) was reached. Negative values are returned for End of File and End of Record, but their precise values(or value) are not specified in the Fortran standard. A positive value indicates a serious problem, but the precise meaning associated with a given number is system dependent and defined in the specific compiler manual. The presence of this argument will prevent termination of a program if an error occurs when opening the file. It is up to you to handle the error.
Specifies a label to which the program will branch if any error is detected when opening the file.
To open a brand new file:
Note: The plain 13 is the same as saying unit=13. Also, the computer will automatically assume that the file is to be of the form "formatted". If 'results.out' already exists, an error condition will exist. To connect an existing unformatted file for data entry:
To include error checks in your open statement.
Open(28,file='data.in',form='formatted',err=450,iostat= & ierr)
Then to actually do the checks, you should write something like this:
450 print *, 'Error detected. Iostat =',ierr
This could be followed by logical tests to act on the error condition based on the specific meaning of the error flag for your computer .
examples: charvr90.f and grades1.f
Written by Jason Wehr : firstname.lastname@example.org and Maintained by John Mahaffy : email@example.com