在Fortran中写入然后从同一个文件中读取

时间:2017-05-26 14:57:33

标签: fortran

当我想编写一个代码时,我想用它来打开一个txt文件,在其中写一个实数并将数字传递给我代码中的变量。

我的代码如下:

open (unit = 22, file = x_file, status = 'old')
write(22, *) 1.2345

do while (ios == 0)
   read(22,*, iostat=ios) reader
end do

write(*,*) reader

编译变量" reader"得到一个非常小的数字(2.2460454138806765E-314)。 删除行后

write(22, *) 1.2345

直接在txt文件中翻转我的变量" reader"变成1.2345。可能是什么原因?

1 个答案:

答案 0 :(得分:1)

将值写入文件

write(22, *) 1.2345

文件位于末尾。

到达文件末尾时你的陈述

read(22,*, iostat=ios) reader

什么都不读,将ios设置为文件末尾非零值结束进一步。 reader的值未定义!

如果iostat不为零,则不能在输入列表中使用变量值。

你想要:

open ( unit = 22, file = x_file, status = 'old' )

write(22, *) 1.2345

rewind(22)

read(22,*, iostat=ios) reader
if (ios/=0) stop

write(*,*) reader

相关问题