Implementing getarg subroutine call

霸气de小男生 提交于 2021-02-04 21:42:43

问题


I've written a program in F90 which reads in a few input arrays from text files and then combines them through a function to a single output file. One of the input files is named for the day the data was collected using MMDDYY.tuvr and the output file is then named MMDDYY.fxi . I'd like to be able to input the MMDDYY of the data in the command line when running the program instead of having to manually change the code and compile each time, which is why I'm attempting to use getarg, but I cannot seem to make it work properly. The code im attempting to use is listed below (just shows the get arg and the open commands and not the entire program since this is where I'm having trouble):

CHARACTER(len=20) :: arg, tuvrname, fxiname
CALL getarg(1, arg)
IF(LEN_TRIM(arg) == 0) THEN
    print*,'No date provided'
    STOP
ELSE
    tuvrname = TRIM(arg)'.tuvr'
    fxiname = TRIM(arg).'fxi'
ENDIF

OPEN(1, file = tuvrname, status='old', action='read')
....
OPEN(4, file = fxiname, status='replace', action='write')

I also tried just using two separate getarg commands and entering MMDDDYY.tuvr MMDDYY.fxi in the command line and the program ran, but it could not seem to find my TUVR file as the output was empty.


回答1:


I am not really experienced in using getarg. I use get_command_argument from Fortran 2003. I think you just forgot to use // to concatenate the strings.

CHARACTER(len=20) :: arg, tuvrname, fxiname
CALL getarg(1, arg)
IF(LEN_TRIM(arg) == 0) THEN
    print*,'No date provided'
    STOP
ELSE
    tuvrname = TRIM(arg)//'.tuvr'
    fxiname = TRIM(arg)//'.fxi'
ENDIF

print *, tuvrname, fxiname

end

or

CHARACTER(len=20) :: arg, tuvrname, fxiname
if (command_argument_count()<1) then
  stop "Provide the file name."
end if
CALL get_command_argument(1, value=arg)

tuvrname = TRIM(arg)//'.tuvr'
fxiname = TRIM(arg)//'.fxi'

print *, tuvrname, fxiname

end


来源:https://stackoverflow.com/questions/15369096/implementing-getarg-subroutine-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!