André Almeida
André Almeida

Reputation: 509

Unexpected End of File in FORTRAN?

I've been looking on the net but I haven't found any solution yet.

I'm trying to read from a .neu (neutral format) file, which is a type of file generated by the Gambit software, and contains information about a mesh used in CFD calculations.

The file goes like this:

        CONTROL INFO 2.4.6
** GAMBIT NEUTRAL FILE
S1
PROGRAM:                Gambit     VERSION:  2.4.6

     NUMNP     NELEM     NGRPS    NBSETS     NDFCD     NDFVL
    120847    240234         1         1         2         2
ENDOFSECTION
   NODAL COORDINATES 2.4.6
         1  0.00000000000e+000  2.50000000000e+001
         2  0.00000000000e+000  0.00000000000e+000
         3  0.00000000000e+000  2.49000000000e+001
         4  0.00000000000e+000  2.48000000000e+001
        ... 

I'm trying to read the following line:

120847    240234         1         1         2         2

Here's my code:

PROGRAM readGAMBIT

    INTEGER, DIMENSION(6) :: mdata
    LOGICAL :: lexist
    INTEGER :: i

    INQUIRE (FILE="mesh.neu", EXIST=lexist)
    IF (.not.lexist) THEN
        STOP "**** mesh.neu does not exist ****"
    ENDIF

    OPEN(77,file="mesh.neu")

    DO i = 1, 6
        READ(1,*) ! Ignores the 1st 6 lines
    ENDDO

    READ(1,*) mdata(1), mdata(2), mdata(3), mdata(4), mdata(5), mdata(6)
    PRINT*, mdata(1)

    CLOSE(77)

END PROGRAM readGAMBIT

However, when I run this code I get "Fortran runtime error: End of file" . The file only ends after a couple thousand lines however.

What am I doing wrong? Why is the program thinking that the file only ends at the first line?

Upvotes: 2

Views: 7741

Answers (1)

MatCross
MatCross

Reputation: 389

Here's what my compiler says:

> nagfor -C=all -C=undefined readGAMBIT.f90 && ./a.out 
....
Runtime Error: End of file on unit 1
Program terminated by I/O error on unit 1 (File="fort.1",Formatted,Sequential)
Abort (core dumped)

It is pretty clear that you are associating mesh.neu to unit 77 but then attempting to read from unit 1.

If your compiler supports the Fortran 2008 NEWUNIT= specifier on the OPEN statement then I suggest you use that, as follows (and prettified)

Program readgambit
  Integer, Dimension (6) :: mdata
  Logical :: lexist
  Integer :: i, newunit
  Inquire (File='mesh.neu', Exist=lexist)
  If (.Not. lexist) Then
    Stop '**** mesh.neu does not exist ****'
  End If
  Open (File='mesh.neu', Newunit=newunit)
  Do i = 1, 6
    Read (newunit, *) ! Ignores the 1st 6 lines
  End Do
  Read (newunit, *) mdata(1), mdata(2), mdata(3), mdata(4), mdata(5), mdata(6)
  Print *, mdata(1)
  Close (newunit)
End Program

which for me gives

> nagfor -C=all -C=undefined readGAMBIT.f90 && ./a.out
...
 120847

Upvotes: 4

Related Questions