尝试将不正确的值读入变量会改变它的值吗?
Will the attempt to read an improper value into a variable change its value?
如果存在 iostat
关键字,则程序不会在出现 I/O 错误时停止。然后,如果我尝试将不正确的值读入标量变量,i
说,这个变量会保持不变吗?以下似乎有效:
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 中的这种行为吗?
不是,读取失败后变量的值未定义。
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;
如果存在 iostat
关键字,则程序不会在出现 I/O 错误时停止。然后,如果我尝试将不正确的值读入标量变量,i
说,这个变量会保持不变吗?以下似乎有效:
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 中的这种行为吗?
不是,读取失败后变量的值未定义。
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;