Will the attempt to read an improper value into a

2019-02-25 22:44发布

If the iostat keyword is present, a program does not stop if an I/O error occurs. Then, if I try to read an improper value into a scalar variable, i say, will this variable remain unchanged? The following seems to work:

program test
   integer :: i, stat

   i = 1

   do
      write (*, "('i = ')", advance='no')
      read (*, *, iostat=stat) i

      if (stat .eq. 0) then
         write (*, "('Valid integer. i has been set to ', I0)") i
      else
         write (*, "('Bad integer. i is still ', I0)") i
      end if
   end do
end program test

Can I rely on this behavior in Fortran 2003?

1条回答
乱世女痞
2楼-- · 2019-02-25 23:30

No, the value of the variable after an unsuccessful read is undefined.

Fortran 2008, 9.11.2

If an error condition occurs during execution of an input/output statement that contains either an ERR= specifier or an IOSTAT= specifier then:

...

if the statement is a READ statement or the error condition occurs in a wait operation for a transfer initiated by a READ statement, all input items or namelist group objects in the statement that initiated the transfer become undefined;

查看更多
登录 后发表回答