更新时间:2023-02-22 12:17:28
如果您想在命令行上将参数提供给您的程序,请使用(自 Fortran 2003 起)标准内在子例程 GET_COMMAND_ARGUMENT
.这样的事情可能会奏效
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
注意:
get_command_argument
的第二个参数是一个字符变量,您必须将其解析为真实的(或其他).另请注意,我只允许 args
数组的每个元素中包含 12 个字符,您可能想要摆弄这个.read
不用于读取 Fortran 程序中的命令行参数.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.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.