How to know that we reached EOF in Fortran 77?

谁说我不能喝 提交于 2019-12-25 03:02:19

问题


So let's assume that I have the following subroutine:

         subroutine foo(a_date)
         character*10 dummy, a_date
         open(unit=1,file='ifile.txt',status='old')
         read(1, 100) dummy
   100   format(A10)
         a_date = dummy
         return
         end

which only reads a line from the file. But I want to read all the lines recursively. So when I call the subroutine recursively in my main procedure, I get an error after reaching EOF. So is there a way to prevent it so that the program knows when I reach EOF? Basically, I want to be able to know when I reach EOF.


回答1:


Here are two methods. I refuse to teach the obsolete Fortran 77 which shouldn't have been used or taught in 25 years+, but the first method should work in any version of Fortran from 77 onwards

Method 1:

ijb@ianbushdesktop ~/stackoverflow $ cat data.dat 
1
2
3
ijb@ianbushdesktop ~/stackoverflow $ cat end.f90
Program eof
  Implicit None
  Integer :: data
  Open( 10, file = 'data.dat' )
  Do
     Read( 10, *, End = 1 ) data
     Write( *, * ) data
  End Do
1 Write( *, * ) 'Hit EOF'
End Program eof
ijb@ianbushdesktop ~/stackoverflow $ gfortran -std=f2003 -Wall -Wextra -O -fcheck=all end.f90 
ijb@ianbushdesktop ~/stackoverflow $ ./a.out
           1
           2
           3
 Hit EOF

Method 2:

This needs F2003, but that's what you should be using these days

ijb@ianbushdesktop ~/stackoverflow $ cat data.dat 
1
2
3
ijb@ianbushdesktop ~/stackoverflow $ cat end2.f90
Program eof
  Use, intrinsic :: iso_fortran_env, Only : iostat_end
  Implicit None
  Integer :: data, error
  Open( 10, file = 'data.dat' )
  Do
     Read( 10, *, iostat = error ) data
     Select Case( error )
     Case( 0 )
        Write( *, * ) data
     Case( iostat_end )
        Exit
     Case Default
        Write( *, * ) 'Error in reading file'
        Stop
     End Select
  End Do
  Write( *, * ) 'Hit EOF'
End Program eof
ijb@ianbushdesktop ~/stackoverflow $ gfortran -std=f2003 -Wall -Wextra -O -fcheck=all end2.f90 
ijb@ianbushdesktop ~/stackoverflow $ ./a.out
           1
           2
           3
 Hit EOF



回答2:


In Fortran 77 you use the END=label attribute, it instructs the program to go the given label when the end of file condition is triggered. Basically it works like a GO TO statement triggered by the READ statement.



来源:https://stackoverflow.com/questions/54399425/how-to-know-that-we-reached-eof-in-fortran-77

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!