Fortran 如何在输入正常的情况下调试坏整数错误

Fortran 如何在输入正常的情况下调试坏整数错误,fortran,gfortran,Fortran,Gfortran,我有一个fortran代码,我引导它从命令中读取整数。我在代码中定义这些输入参数是整数,并且我在命令中给出了整数,如下所示: /密度-o数据-s1-k8-b20 但它显示了一个错误 Fortran运行时错误:列表输入中项目1的整数错误。 那代码怎么了?请帮帮我 integer s,b,k if(option == "-o") then read(value,*) outputfile else if(option == "-s") then read(value,*) s els

我有一个fortran代码,我引导它从命令中读取整数。我在代码中定义这些输入参数是整数,并且我在命令中给出了整数,如下所示: /密度-o数据-s1-k8-b20 但它显示了一个错误 Fortran运行时错误:列表输入中项目1的整数错误。 那代码怎么了?请帮帮我

integer s,b,k
if(option == "-o") then
    read(value,*) outputfile
else if(option == "-s") then
    read(value,*) s
else if(option == "-k") then
    read(value,*) k
else if(option == "-b") then
    read(value,*) b

您应该始终提供一个最小的工作程序,而不仅仅是一段源代码。在您的代码中,不清楚命令行参数是如何存储到“option”和“value”中的。一个有效的例子可以是:

program cmd_read
!
implicit none
!
integer, parameter :: MAXARG=8                   ! There are four pairs of arguments
character(len=1024), dimension(MAXARG)   :: args
character(len=2)   , dimension(MAXARG/2) :: options = (/'-o', '-s' , '-k', '-b' /)
character(len=1024)                      :: outputfile = ' '
integer :: iarg      ! number of arguments
integer :: i         ! dummy loop index
integer :: ios       ! io error status
integer :: s=0, k=0, b==   ! command line integer values, provide default values
!
args = ' '           ! ensure that they are blank
iarg = COMMAND_ARGUMENT_COUNT()   ! Get number of arguments
if(iarg >  0) then   ! Always keep in mind that there might be no arguments
   do i = 1, iarg
      call GET_COMMAND_ARGUMENT (i, args(i) )
   enddo
endif
!
do i= 1, iarg, 2      ! argument names are odd arguments
!                     ! Arguments might be in different sequence
   if(args(i)==options(1)) then      ! An alternative is the "CASE" construct
      outputfile = args(i+1)
   elseif(args(i)==options(2)) then
      read(args(i+1),*, iostat=ios) s
      if(ios/=0) then
         write(*,*) ' Error on -s'
      endif
   elseif(args(i)==options(3)) then
      read(args(i+1),*) k
   elseif(args(i)==options(4)) then
      read(args(i+1),*) b
   endif
enddo
write(*,'(a, i4, i4, i4)') outputfile(1:len_trim(outputfile)), s, k, b
!
end program cmd_read
此程序尚未检查命令行参数是否存在,并且是否在合理的数值范围内。'-s'上的I/O错误检查只是一个快速而肮脏的示例,用于捕获诸如-s1.8或-s文本之类的内容