Я думаю, вы должны прочитать правильные флаги для вашего открытого заявления. Ключевое слово recl
используется для файлов прямого доступа. Это означает, что каждая запись имеет одинаковую длину, поэтому, если вам нужна 64-я запись, программа сразу узнает, где находится эта запись.
Если ваш файл имеет длину записи, я ее не вижу. Как я сказал в своем комментарии, у меня есть сильное подозрение, что fileln
- это длина файла в байтах (вы не сказали). Таким образом, при первом чтении будет прочитана первая запись, представляющая собой весь файл, а затем проанализированная в переменной N
- которая может принимать только одно целое число, а все остальное затем отбрасывается.
И затем он пытается прочитать следующие fileln
байты после конца файла, что приводит к ошибке «конец файла».
В вашем случае я бы добавил action="READ"
и form="FORMATTED"
ключевые слова в файле open
оператор. И я всегда нахожу полезным не только искать код ошибки, но и сообщение об ошибке iomsg
.
Для чего оно стоит, вот мое предложение о том, как прочитать файл:
program readfile
implicit none
integer :: u ! File handle
integer :: ios
character(len=100) :: iom, line
integer :: N, NNodes, U_Mean, L
real :: Turb_Int
character(len=20) :: Type_GP
real, allocatable :: node_values(:)
character(len=10), allocatable :: node_names(:)
character(len=*), parameter :: inputfile = 'data.txt'
! newunit, instead of unit, creates a new unique
! file unit number. You could also set u to 100
! and then use unit=u
open(newunit=u, file=inputfile, action='READ', &
status='OLD', form='FORMATTED', iostat=ios, &
iomsg=iom)
if (ios /= 0) then
print *, "Error opening file:"
print *, iom
stop 1
end if
! Read the header. I'm always reading 'line' when
! I expect there to be just the next keyword
! in the line. You might want to check for that
read(u, *) line ! Hopefully 'N'
read(u, *) N
read(u, *) line ! Hopefully "NNODES"
read(u, *) NNodes
! I assume that NNODES stands for the number
! of columns later in the file.
! So here I'm also allocating the arrays.
allocate(node_values(NNodes))
allocate(node_names(NNodes))
read(u, *) line ! Hopefully TURB_INT
read(u, *) Turb_Int
read(u, *) line ! Hopefully U_MEN
read(u, *) U_Mean
read(u, *) line ! Hopefully TYPE_GP
read(u, *) Type_GP
read(u, *) line ! Hopefully L
read(u, *) L
read(u, *) line ! Hopefully WIND_TURB
read(u, *) node_names
! Just print out what we got from the header so far to see
! everyting's right
write(*, '(A, I0)') "N = ", N
write(*, '(A, I0)') "NNodes = ", NNodes
write(*, '(A, F5.1)') "Turb_Int = ", Turb_Int
write(*, '(A, I0)') "U_Mean = ", U_Mean
write(*, '(A, A)') "Type_GP = ", trim(Type_GP)
write(*, '(A, I0)') "L = ", L
write(*, '(A, *(A))') node_names
! Now read the data itself. In this case I'm just reading it
! line by line, print it out, and forget it again
! until the end of the file.
data_loop : do
read(u, *, iostat=ios, iomsg=iom) node_values
if (ios /= 0) exit data_loop
print *, node_values
end do data_loop
end program readfile