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% f95 -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% f95 -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 95 programs can use the intrinsic function TRIM, or the older FORTRAN 77 library routine LNBLNK()) 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.
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.