Search code examples
fortranfortran90gfortranfortran77fortran95

Fortran I/O, first read is EOF?


I'm trying to use some old FORTRAN code with some new Java code which works in Windows(as an exe) but not in OS X. I try to build it in eclipse and I get

make: *** [all] Segmentation fault: 11

so I go to terminal and do it that way, even different compilers but still the same result:

Running OS X 10.7.5 and gfortran-4.2 made with standard -c and -o commands

Program foo
open(unit = 1, file = 'variables.txt',IOSTAT= iost)
    write(*,*)iost
    read(1,*) P
    write(*,*)P
...
end program foo

the program builds manually but the output is:

0
At line 13 of file Cubic42.f
Fortran runtime error: End of file

I have also seen this error:

list in: end of file
apparent state: unit 88 named variables
last format: list io
lately reading sequential formatted external IO
Abort

It shows that IOSTAT returns 0, which means the file is good? But it will not read the file, even if I change the unit# to say, 88.. and even if I change the CR to mac, windows, or unix.

It seems to be only a problem with the input/output, if i hardcode variables, for example, the program works.

Any ideas on how to fix this? Thanks in advance.

Edit1 Here is the variables.txt file

-9999
15.6
500
150.9
48.98
0.000

there is a carriage return at the end, and it doesn't matter if i change the -9999 to positive

Edit2

I deleted the text file from the directory and remade the .f to look like this:

program foo
   implicit none
    real a, b, c, d
   open(unit = 1, file = 'variables.2txt', action='write')
   write(1, *) -6666
   write(1, *) 6.15
    write(1, *) -321
    write(1, *) 5.16
   close(1)

    open(unit = 2, file = 'variables.2txt', action='read',form='FORMATTED')
    read(2, *) a
            write(*,*) a
    read(2, *) b
            write(*,*) b
    read(2, *) c
            write(*,*) c
    read(2, *) d
            write(*,*) d
    close(2)

end program foo

Then I compiled it.
Output is:

  -6666.000    
   6.150000    
  -321.0000    
   5.160000 

as expected, but variables.2txt is nowhere to be found! I'm very confused, please help.

Edit3 I have found the phantom file. It is located at /users/me/phantom.txt So the question is, how do I make the file save in the same directory as the executable?


Solution

  • I get a very similar error message to yours

               0
    At line 4 of file proba.f (unit = 1, file = 'variables.txt')
    

    when running your code on Linux with a file variables.txt where I explicitly set the end of line characters according to the old MAC convention to ^M (instead of Unix's ^J). So, I guess, it is an EOL-convention problem. You could eventually try to write two lines to a file and investigate that file in order to decide which EOL-convention gfortran expects on your system:

    program foo
      implicit none
      open(unit = 1, file = 'variables.txt', action='write')
      write(1, *) -9999
      write(1, *) 15.6
      close(1)
    end program foo
    

    Also, I'd definitely go for a more recent gfortran compiler (current stable is version 4.7.2).