Getting fortran runtime error: end of file

user3444034 picture user3444034 · Mar 22, 2014 · Viewed 47.8k times · Source

I have recently learned how to work with basic files in Fortran and I assumed it was as simple as:

open(unit=10,file="data.dat")
read(10,*) some_variable, somevar2
close(10) 

So I can't understand why this function I wrote is not working. It compiles fine but when I run it it prints:

fortran runtime error:end of file

Code:

Function Load_Names()

character(len=30) :: Staff_Name(65)
integer :: i = 1

open(unit=10, file="Staff_Names.txt")

do while(i < 65)

read(10,*) Staff_Name(i)
print*, Staff_Name(i)
i = i + 1

end do

close(10)
end Function Load_Names

I am using Fortran 2008 with gfortran.

Answer

High Performance Mark picture High Performance Mark · Mar 22, 2014

A common reason for the error you report is that the program doesn't find the file it is trying to open. Sometimes your assumptions about the directory in which the program looks for files at run-time will be wrong.

Try:

  • using the err= option in the open statement to write code to deal gracefully with a missing file; without this the program crashes, as you have observed;

or

  • using the inquire statement to figure out whether the file exists where your program is looking for it.