Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use command line arguments in Fortran?

GCC version 4.6

The Problem: To find a way to feed in parameters to the executable, say a.out, from the command line - more specifically feed in an array of double precision numbers.

Attempt: Using the READ(*,*) command, which is older in the standard: Program test.f -

PROGRAM MAIN  
     REAL(8)    :: A,B  
     READ(*,*) A,B
     PRINT*, A+B, COMMAND_ARGUMENT_COUNT()
END PROGRAM MAIN

The execution -

$ gfortran test.f
$ ./a.out 3.D0 1.D0

This did not work. On a bit of soul-searching, found that

$./a.out
3.d0,1.d0
   4.0000000000000000                0

does work, but the second line is an input prompt, and the objective of getting this done in one-line is not achieved. Also the COMMAND_ARGUMENT_COUNT() shows that the numbers fed into the input prompt don't really count as 'command line arguments', unlike PERL.

like image 491
Debanjan Basu Avatar asked Dec 12 '12 16:12

Debanjan Basu


People also ask

How do you pass arguments in Fortran?

There are two ways to pass individual arguments: By value, which passes the argument's value. By reference, which passes the address of the arguments. On systems based on IA-32 architecture, Fortran, C, and C++ use 4-byte addresses.

How do I run a Fortran command line?

To open a command prompt, click on Start menu, choose Accessories and then Command Prompt, or choose Run and type "cmd". In the black window that opens, you can use gfortran to compile your Fortran code (assuming your program is file code. f95 in the current directory): gfortran code.

What is argument in Fortran?

The type of these formal arguments is defined by some combination of default, type statements, IMPLICIT statements, and DIMENSION statements. The number of formal arguments must be the same as the number of actual arguments at the invocation of this function subprogram. A function can assign values to formal arguments.


1 Answers

If you want to get the arguments fed to your program on the command line, use the (since Fortran 2003) standard intrinsic subroutine GET_COMMAND_ARGUMENT. Something like this might work

PROGRAM MAIN  
     REAL(8)    :: A,B
     integer :: num_args, ix
     character(len=12), dimension(:), allocatable :: args

     num_args = command_argument_count()
     allocate(args(num_args))  ! I've omitted checking the return status of the allocation 

     do ix = 1, num_args
         call get_command_argument(ix,args(ix))
         ! now parse the argument as you wish
     end do

     PRINT*, A+B, COMMAND_ARGUMENT_COUNT()
END PROGRAM MAIN

Note:

  • The second argument to the subroutine get_command_argument is a character variable which you'll have to parse to turn into a real (or whatever). Note also that I've allowed only 12 characters in each element of the args array, you may want to fiddle around with that.
  • As you've already figured out read isn't used for reading command line arguments in Fortran programs.

Since you want to read an array of real numbers, you might be better off using the approach you've already figured out, that is reading them from the terminal after the program has started, it's up to you.

like image 88
High Performance Mark Avatar answered Sep 28 '22 03:09

High Performance Mark