shyammp
shyammp

Reputation: 3

Accesing filename passed to Fortran executable within its program

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

Answers (1)

brady
brady

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

Related Questions