2016-06-07 57 views
2

我运行从Fortran源编译的程序:检查程序的参数是否为空?

./a.out N t 

这里N和T定义了两个正整数。下面是该代码:

character(len=10) :: arg 
    call get_command_argument(1, arg) 
    read(arg,'(I10)') N 
    call get_command_argument(2, arg) 
    read(arg,'(I10)') t 

接下来,我想这样做以下:如果我不输入既不ñ也不吨(即,如果arg,1是空的),那么程序提出进入他们通过程序(我不知道Fortran中scanf的模拟是什么)。如何明确地做到这一点,你能帮忙吗?

回答

4

如果我理解正确的话,你正在寻找的东西像

character(len=10) :: arg 
call get_command_argument(1, arg) 
if (trim(arg) == '') then 
    write(*,*) 'Please enter N:' 
    read(*,*) N 
else 
    read(arg,'(I10)') N 
end if 

call get_command_argument(2, arg) 
if (trim(arg) == '') then 
    write(*,*) 'Please enter t:' 
    read(*,*) t 
else 
    read(arg,'(I10)') t 
end if 
4

1)你的代码(即get_command_argument)是没有办法的Fortran 77,Fortran语言,但2003年

2)只要使用command_argument_count()找出你有多少争论了。

if (command_argument_count()==0) then 
    do whatever you need to do 
+0

但对于FORTRAN 77?我不相信在f77的情况下不可能检查这个论点是否是空的。 –

+1

@JohnTaylor **你的**代码是Fortran 2003,所以没有要求Fortran 77.在Fortran 77中,你必须使用各种非标准的扩展(比如'getarg'和'iargc'),但是没有必要结合它们与'get_command_argument()' –

+0

因此,如果参数的数量是1,比我需要编写 if(command_argument_count()== 1)? –