Fortran运行时错误:gfortran中的文件结束
当我使用gfortran test.f95运行此程序时,它显示一个错误Fortran运行时错误:gfortran中的文件结束,fortran,gfortran,Fortran,Gfortran,当我使用gfortran test.f95运行此程序时,它显示一个错误 At line 10 of file test.f95 (unit = 15, file = 'open.dat') Fortran runtime error: End of file 有人能告诉我这里怎么了吗 implicit none integer:: a,b,c,ios open(unit=15,file="open.dat",status='unknown', action='readwrite',iost
At line 10 of file test.f95 (unit = 15, file = 'open.dat')
Fortran runtime error: End of file
有人能告诉我这里怎么了吗
implicit none
integer:: a,b,c,ios
open(unit=15,file="open.dat",status='unknown', action='readwrite',iostat=ios)
open(unit=16,file="open.out",status="unknown",action='write')
do a=1,100
write(15,*)a
end do
do c=1,45,1
read(15,*)
read(15,*)b
write(16,*)b
end do
stop
end
确保使用“倒带”/fseek
你也可以通过倒带(15)来实现这一点。我根本不推荐fseek。标准Fortran已经足够强大了。仍然坚持非标准和不可移植的
fseek
:-)C传统;)但是,毕竟,倒带是最终代码中的“选择之一”。)谢谢大家。我用倒带解决了我的问题。请对所有Fortran问题使用tag。如果您将错误消息复制并粘贴到标题中,系统将告诉您此类问题已经存在。可能它已经告诉过你了,所以你不得不稍微改变一下。请,你真的应该先阅读现有的问题。另外,向我们显示您正在读取的数据非常重要。顺便说一句,在结束之前传递状态='unknown'
和使用停止
,是没有意义的。您可以直接删除它。您也不应该指定iostat
,然后不处理结果。
implicit none
integer :: a, b, c, ios, ierr
open( unit=15, file="open.dat", action='readwrite', iostat=ios)
open( unit=16, file="open.out", action='write')
do a=1,100
write(15,*)a
end do
! Here we need to get back to beginning of file
! otherwise, you try to read where you finished writing
! at the end of file
! CALL FSEEK(UNIT, OFFSET, WHENCE[, STATUS])
! https://gcc.gnu.org/onlinedocs/gfortran/FSEEK.html
! call fseek(15, 0, 0, ierr)
! you can also use rewind file
! https://www.fortran.com/F77_std/rjcnf0001-sh-12.html
rewind(15)
do c=1,45,1
read(15,*)
read(15,*)b
write(16,*)b
end do
end