Getting fortran runtime error: end of file

前端 未结 6 1969
借酒劲吻你
借酒劲吻你 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条回答
  •  -上瘾入骨i
    2020-12-19 04:43

    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 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
    

提交回复
热议问题