Fortran运行时错误:gfortran中的文件结束

时间:2017-09-29 07:02:43

标签: 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',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

1 个答案:

答案 0 :(得分:0)

请务必使用 快退 / fseek

更改文件中的位置
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