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.
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:
Method 2:
This needs F2003, but that's what you should be using these days
In Fortran 77 you use the
END=label
attribute, it instructs the program to go the givenlabel
when the end of file condition is triggered. Basically it works like aGO TO
statement triggered by theREAD
statement.