2017-09-29 77 views
2

文件的结束当我使用gfortran test.f95运行这个程序,它显示了一个错误Fortran运行时错误:gfortran

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 
+0

请使用标签更改位置[标签:FORTRAN]所有的Fortran问题。如果您将错误消息完全复制并粘贴到标题中,系统会告诉您这样的问题已经存在。它可能已经告诉过你了,所以你必须稍微改变它。请你真的应该先阅读现有的问题。另外,向我们展示您正在阅读的数据非常重要。 –

+0

顺便说一句,在传递'status ='unknown''并在'end'前面使用'stop'没有意义。你可以删除它。 –

+1

你也不应该指定'iostat'然后不处理结果。 – agentp

回答

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 
+1

你也可以用**倒带(15)** – cup

+3

我不推荐fseek。标准Fortran足够强大。 –

+0

仍然拘泥于非标准和非便携式的'fseek' :-) –