Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran read语句';s iostat参数返回代码5001_Io_Fortran_Gfortran - Fatal编程技术网

Fortran read语句';s iostat参数返回代码5001

Fortran read语句';s iostat参数返回代码5001,io,fortran,gfortran,Io,Fortran,Gfortran,我正在用fortran编写一个计算BMI的小代码,但我对read语句的iostat参数有一个问题。整个守则如下: program calculate_bmi ! Simple BMI calculator implicit none character :: unit_system character :: height_unit*15, weight_unit*10 real :: height, weight, bmi integer :: ierror pr

我正在用fortran编写一个计算BMI的小代码,但我对read语句的iostat参数有一个问题。整个守则如下:

program calculate_bmi
  ! Simple BMI calculator
  implicit none
  character :: unit_system
  character :: height_unit*15, weight_unit*10
  real :: height, weight, bmi
  integer :: ierror


  print *, 'Would you like to use imperial or metric system?'
  systemloop: do
    print *, 'Press [I] for imperal system or press [M] for metric system.'
    read *, unit_system
    if (unit_system == 'i' .or. unit_system == 'I' .or. &
     unit_system == 'm' .or. unit_system == 'M') exit systemloop
  end do systemloop

  if (unit_system == 'i' .or. unit_system == 'I') then
    height_unit='inches'
    weight_unit = 'pounds'
  else
    height_unit = 'centimeters'
    weight_unit= 'kilograms'
  end if

  print *, 'Type in your height in ', height_unit
  read (*, iostat = ierror) height
  if (ierror /= 0) then
    print *, 'Invalid input for height!'
    call exit
  end if
  print *, 'Type in your weight in ', weight_unit
  read (*, iostat = ierror) weight
  if (ierror /= 0) then
    print *, 'Invalid input for weight!'
    call exit
  end if

  if (unit_system == 'i' .or. unit_system == 'I') then
    bmi = 703 * weight / (height**2)
  else
    bmi = weight / ((height/100)**2)
  end if

  print *, 'Your BMI is: '
  write (*, 1) NINT(bmi*10)/10.0
  1 format(f6.1)
end program calculate_bmi
此代码块出现问题:

 print *, 'Type in your height in ', height_unit
  read (*, iostat = ierror) height
  if (ierror /= 0) then
    print *, 'Invalid input for height!'
    call exit
  end if
  print *, 'Type in your weight in ', weight_unit
  read (*, iostat = ierror) weight
  if (ierror /= 0) then
    print *, 'Invalid input for weight!'
    call exit
  end if
iostat总是返回5001作为代码,read语句不等待输入,只是终止程序。我在谷歌上做了一些搜索,但没有真正弄清楚到底是什么问题。如果我从read语句中删除iostat,代码就可以正常工作,但是在这种情况下,我没有任何错误处理和防止错误输入的方法


我对fortran有点陌生,所以请让我知道我在哪里犯了错误。另外,我使用Gfortran作为编译器。

read语句通常需要两个位置参数,一个用于输入单元(或
*
将其声明为标准输入),另一个用于格式(或再次使用
*
自行计算)

不带括号,即带

read *, some_var
该命令自动默认为标准输入流,因此单个
*
仅适用于该格式。但一旦使用括号,就需要声明输入单位和格式

read
语句,以及
open
close
write
,现在支持一个
iomsg
参数,该参数是一个字符串,为错误提供了可读的解释

尝试如下修改:

character(len=100) :: ioerrmsg
...
read (*, *, iostat=ioerror, iomsg=ioerrmsg) weight
if (ioerror /= 0) then
    print *, "Encountered Error:"
    print *, ioerrmsg
    call exit
end if
这会给你一些关于发生了什么的提示

干杯


PS:如果有人知道默认情况下
iomsg
字符串的长度,请在评论中告诉我。但长度100通常对我来说是可行的。

我觉得自己太愚蠢了,以至于错过了一个参数。非常感谢您的回答和解释,它解决了问题!消息字符串的长度无关紧要。重要的是接收它的变量的长度
iomsg=str
将填充或截断sting以适应
str
@evets是的,但我想知道
iomsg
参数的最短长度,以便它不会被截断。这并不紧急,但我想知道。@chw21,我不认为有可移植的答案,因为Fortran标准规定了
iomsg
值的长度。OTOH,编译器编写者似乎倾向于简洁地处理错误消息。出于实际目的,我怀疑您可以使用长度为128的字符串。