Getting fortran runtime error: end of file

前端 未结 6 1979
借酒劲吻你
借酒劲吻你 2020-12-19 03:54

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,         


        
6条回答
  •  情书的邮戳
    2020-12-19 04:30

    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
    

提交回复
热议问题