2016-06-07 50 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)? –