gpt4 book ai didi

input - 将不正确的值读入变量的尝试会更改其值吗?

转载 作者:行者123 更新时间:2023-12-03 07:42:05 26 4
gpt4 key购买 nike

如果存在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;

关于input - 将不正确的值读入变量的尝试会更改其值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28452661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com