尝试将不正确的值读入变量会改变其价值吗? [英] Will the attempt to read an improper value into a variable change its value?

查看:95
本文介绍了尝试将不正确的值读入变量会改变其价值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果存在 iostat 关键字,如果发生I / O错误,程序不会停止。那么,如果我尝试将一个不正确的值读入一个标量变量,那么这个变量会保持不变吗? i 以下内容似乎有效:

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

可以在Fortran 2003中依靠这种行为吗?

Can I rely on this behavior in Fortran 2003?

推荐答案

否,未成功读取后变量的值未定义。

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

Fortran 2008,9.11.2

Fortran 2008, 9.11.2


如果
执行包含ERR =
说明符或IOSTAT =说明符的输入/输出语句时发生错误条件,则:

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

...

如果语句是READ语句o r在
a等待操作中发生错误条件是由READ语句发起的,所有
输入项或名称组合对象在发起
的语句中传输变为未定义;

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;

这篇关于尝试将不正确的值读入变量会改变其价值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆