The file system does not have any automatic facility to associate a logical unit number in a Fortran program with a physical file.
However, there are several satisfactory ways to communicate file names to a Fortran program.
The library routine getarg(3F) can be used to read the command-line arguments at runtime into a character variable. The argument is interpreted as a file name and used in the OPEN statement FILE= specifier:
demo% cat testarg.f CHARACTER outfile*40 C Get first arg as output file name for unit 51 CALL getarg(1,outfile) OPEN(51,FILE=outfile) WRITE(51,*) 'Writing to file: ', outfile END demo% f77 -silent -o tstarg testarg.f demo% tstarg AnyFileName demo% cat AnyFileName Writing to file: AnyFileName demo%
Similarly, the library routine getenv(3F) can be used to read the value of any environment variable at runtime into a character variable that in turn is interpreted as a file name:
demo% cat testenv.f CHARACTER outfile*40 C Get $OUTFILE as output file name for unit 51 CALL getenv('OUTFILE',outfile) OPEN(51,FILE=outfile) WRITE(51,*) 'Writing to file: ', outfile END demo% f77 -silent -o tstenv testenv.f demo% setenv OUTFILE EnvFileName demo% tstenv demo% cat EnvFileName Writing to file: EnvFileName demo%
When using getarg or getenv, care should be taken regarding leading or trailing blanks. (FORTRAN 77 programs can use the library function LNBLNK; Fortran 90 programs can use the intrinsic function TRIM.) Additional flexibility to accept relative path names can be programmed along the lines of the FULLNAME function in the example at the beginning of this chapter.
The library routine IOINIT can also be used with f77 to attach logical units to specific files at runtime. IOINIT looks in the environment for names of a user-specified form and then opens the corresponding logical unit for sequential formatted I/O. Names must be of the general form PREFIXnn, where the particular PREFIX is specified in the call to IOINIT, and nn is the logical unit to be opened. Unit numbers less than 10 must include the leading 0. See the Sun Fortran Library Reference, and the IOINIT(3F) man page. (The IOINIT facility is not implemented for f90.)
Example: Associate physical files test.inp and test.out in the current directory to logical units 1 and 2:
First, set the environment variables.
demo$ TST01=ini1.inp demo$ TST02=ini1.out demo$ export TST01 TST02
demo% setenv TST01 ini1.inp demo% setenv TST02 ini1.out
demo% cat ini1.f CHARACTER PRFX*8 LOGICAL CCTL, BZRO, APND, VRBOSE DATA CCTL, BZRO, APND, PRFX, VRBOSE & /.TRUE.,.FALSE.,.FALSE., 'TST',.FALSE. / CALL IOINIT( CCTL, BZRO, APND, PRFX, VRBOSE ) READ(1, *) I, B, N WRITE(2, *) I, B, N END demo%
With environment variables and ioinit, ini1.f reads ini1.inp and writes to ini1.out:
demo% cat ini1.inp 12 3.14159012 6 demo% f77 -silent -o tstinit ini1.f demo% tstinit demo% cat ini1.out 12 3.14159 6 demo%
IOINIT is adequate for most programs as written. However, it is written in Fortran specifically to serve as an example for similar user-supplied routines. Retrieve a copy from the following file, a part of the FORTRAN 77 package installation: /opt/SUNWspro/SC5.0/src/ioinit.f
Another way to associate a physical file with a program's logical unit number is by redirecting or piping the preconnected standard I/O files. Redirection or piping occurs on the runtime execution command.
In this way, a program that reads standard input (unit 5) and writes to standard output (unit 6) or standard error (unit 0) can, by redirection (using <, >, >>, >&, |, |&, 2>, 2>&1 on the command line), read or write to any other named file.
This is shown in the following table:
Table 2-1 csh/sh/ksh Redirection and Piping on the Command Line
Action |
Using C Shell |
Using Bourne or Korn Shell |
---|---|---|
Standard input --read from mydata | myprog < mydata | myprog < mydata |
Standard output --write (overwrite) myoutput | myprog > myoutput | myprog > myoutput |
Standard output -- write/append to myoutput | myprog >> myoutput | myprog >> myoutput |
Redirect standard error to a file | myprog >& errorfile | myprog 2> errorfile |
Pipe standard output to input of another program | myprog1 | myprog2 | myprog1 | myprog2 |
Pipe standard error and output to another program | myprog1 |& myprog2 | myprog1 2>&1 | myprog2 |
See the csh, ksh,and sh man pages for details on redirection and piping on the command line.