In Fortran, files are designated by unit number, which is an integer number. Values from to are reserved for standard I/O, standard error, etc. Usually user-defined files have unit numbers of and above.
Sometimes we need to read from/ write into user defined files. In order for Fortran to be aware of the existence of user-defined files, we need to OPEN them (this is somehow similar to variable declaration).
For example, the statement
open( unit=10, file='data.txt', action='READ')will open the file named 'data.txt'; the mode declaration states that this is an input (read-only) file; the statement will allocate unit number 10 to the file 'data.txt'; from now on, all READ(10,*) will read in data from this file.
The statement
open( unit=11, file='results.txt', action='WRITE')\end{tabular}will open the file named 'results.txt'; if not present, will create one; the mode declaration states that this is an output (writeable) file; the statement will allocate unit number 11 to the file 'results.txt'; from now on, all WRITE(11,*) will write data into this file. The list of arguments must be of standard intrinsic types (unless explicit format is used). Each WRITE statement puts the arguments on a new line; non-advancing WRITE is possible, but we need to use formats. Note that the statement, as is, will wipe out the content of results.txt, should it previously exist.
It is possible to declare action='readwrite' for files which are both I/O.
Also not recommended, the two statements above can be abbreviated as
open( 10, 'data.txt', 'READ') open( 11, 'results.txt', 'WRITE')
The opened files may be closed with
close(10) close(11)The unit number can then be re-used (for a different file) in a subsequent OPEN statement.