8
votes

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.

6
Do you get any output or does it fail on the first read? - M. S. B.

6 Answers

8
votes

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.
3
votes

You can check when a file has ended. It is done with the option IOSTAT for read statement. Try:

Function Load_Names()

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

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

do while(i < 65)
  read(10,*, IOSTAT=iostat) Staff_Name(i)
  if( iostat < 0 )then
   write(6,'(A)') 'Warning: File containts less than 65 entries'
   exit
  else if( iostat > 0 )then
   write(6,'(A)') 'Error: error reading file'
   stop
  end if
  print*, Staff_Name(i)
  i = i + 1
end do

close(10)
end Function Load_Names
3
votes

Using Fortran 2003 standard, one can do the following to check if the end of file is reached:

    use :: iso_fortran_env

    character(len=1024) :: line
    integer :: u1,stat

    open (newunit=u1,action='read',file='input.dat',status='old')

ef: do
      read(u1,'A',iostat=stat) line
      if (stat == iostat_end) exit ef ! end of file
      ...
    end do ef
    close(u1)
1
votes

Thanks for all your help i did fix the code:

Function Load_Names(Staff_Name(65))!Loads Staff Names

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

    open(unit=10, file="Staff_Names.txt", status='old', action='read')!opens file for reading

    do while(i < 66)!Sets Set_Name() equal to the file one string at a time

        read(10,*,end=100) Staff_Name(i)
        i = i + 1

    end do 
    100 close(10)!closes file
    return!returns Value
end Function Load_Names

I needed to change read(10,*) to read(10,*,END=100) so it knew what to do when it came to the end the file as it was in a loop I assume.

0
votes

Then your problem was that your file was a row vector, and it was likely giving you this error immediately after reading the first element, as @M.S.B. was suggesting.

If you have a file with a NxM matrix and you read it in this way (F77):

DO i=1,N
  DO j=1,M
     READ(UNIT,*) Matrix(i,j)
  ENDDO
ENDDO

it will load the first column of your file in the first row of your matrix and will give you an error as soon as it reaches the end of the file's first column, because the loop enforces it to read further lines and there are no more lines (if N<M when j=N+1 for example). To read the different columns you should use an implicit loop, which is why your solution worked:

DO i=1,N
    READ(UNIT,*) (Matrix(i,j), j=1,M)     
ENDDO
0
votes

I am using GNU Fortran 5.4.0 on the Ubuntu system 16.04. Please check your file if it is the right one you are looking for, because sometimes files of the same name are confusing, and maybe one of them is blank. As you may check the file path if it is in the same working directory.