Reputation: 3
I am trying to create a Fortan program, which when compiled to an executable, can take a file as a parameter (e.g., progexe afile).
I need to code the program in such a way that the filename passed (afile) is available within the Fortran program. I cannot figure out how to do this. I am using gfortran in MingW on windows.
Upvotes: 0
Views: 279
Reputation: 2257
Use GET_COMMAND_ARGUMENT. It works for me with gfortran 4.6.3. Example use:
PROGRAM Main
IMPLICIT NONE
CHARACTER(LEN=4096) :: FileName
CALL GET_COMMAND_ARGUMENT(1, FileName)
WRITE(*,*) 'Your file name is ', TRIM(FileName)
STOP
ENDPROGRAM
Compile and run:
brady@rocky:~/tmp$ gfortran -o gcatest gcatest.f90
brady@rocky:~/tmp$ ./gcatest myfile.txt
Your file name is myfile.txt
brady@rocky:~/tmp$
Upvotes: 5