The OPEN statement's FILE= specifier establishes the association of a logical unit to a named, physical file at runtime. This file can be pre-existing or created by the program. See the Sun FORTRAN 77 Language Reference Manual for a full discussion of the OPEN statement.
The FILE= specifier on an OPEN statement may specify a simple file name (FILE='myfile.out') or a file name preceded by an absolute or relative directory path (FILE='../Amber/Qproj/myfile.out'). Also, the specifier may be a character constant, variable, or character expression.
Library routines can be used to bring command-line arguments and environment variables into the program as character variables for use as file names in OPEN statements. (See man page entries for getarg(3F) and getenv(3F) for details; these and other useful library routines are also described in the Fortran Library Reference).
The following example (GetFilNam.f) shows one way to construct an absolute path file name from a typed-in name. The program uses the library routines GETENV, LNBLNK, and GETCWD to return the value of the $HOME environment variable, find the last non-blank in the string, and determine the current working directory:
CHARACTER F*128, FN*128, FULLNAME*128 PRINT*, 'ENTER FILE NAME:' READ *, F FN = FULLNAME( F ) PRINT *, 'PATH IS: ',FN END CHARACTER*128 FUNCTION FULLNAME( NAME ) CHARACTER NAME*(*), PREFIX*128 C This assumes C shell. C Leave absolute path names unchanged. C If name starts with '~/', replace tilde with home C directory; otherwise prefix relative path name with C path to current directory. IF ( NAME(1:1) .EQ. '/' ) THEN FULLNAME = NAME ELSE IF ( NAME(1:2) .EQ. '~/' ) THEN CALL GETENV( 'HOME', PREFIX ) FULLNAME = PREFIX(:LNBLNK(PREFIX)) // & NAME(2:LNBLNK(NAME)) ELSE CALL GETCWD( PREFIX ) FULLNAME = PREFIX(:LNBLNK(PREFIX)) // & '/' // NAME(:LNBLNK(NAME)) ENDIF RETURN END
Compiling and running GetFilNam.f results in:
demo% pwd /home/users/auser/subdir demo% f77 -silent -o getfil GetFilNam.f demo% getfil anyfile /home/users/auser/subdir/anyfile demo%