Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran (90) runtime error: End of file

  1. Oct 8, 2015 #1
    I am using fortran 90 to find the prime numbers (1-100). When I print to the console, everything works. If I try to write to an text file (.out).
    This is from my command line:
    Sues-MacBook-Air:FORTRAN sueparks$ gfortran PRIME.f90
    Sues-MacBook-Air:FORTRAN sueparks$ ./MY_PRIME.out

    Code (Fortran):

    PROGRAM PRIME

    implicitnone

    integer, PARAMETER::M=100
    integer::at, found, i
    logical::is_prime
    integer, dimension(M)::primes! array that will hold the primes

    open (1, file="MY_PRIME.out", status="unknown")


    !print *, "How many primes would you like to find?"
    !read *, num_primes

    primes(1)=2
    at =2
    found =1
    do
        is_prime =.true.! assume prime
       do i =1, found
           if(modulo(at, primes(i))==0)then! if divisible by any other element
                is_prime =.false.! in the array, then not prime.
               exit
           endif
       enddo
       if(is_prime)then
            found = found +1
            primes(found)= at
            print *, at
            WRITE(1,*)"THE FIRST 100 PRIME NUMBERS:" ,AT
            CLOSE(1)
       endif
        at = at +1
       if(found == M)then! stop when all primes are found
           exit
       endif
    enddo

    ENDPROGRAM PRIME

     
     
    Last edited by a moderator: Oct 8, 2015
  2. jcsd
  3. Oct 8, 2015 #2

    Mark44

    Staff: Mentor

    ???
    It looks like you didn't finish the last sentence.
     
  4. Oct 8, 2015 #3

    phinds

    User Avatar
    Gold Member
    2016 Award

    You are closing the output file as soon as you find any prime, so you'll just print one prime in your output file.
     
  5. Oct 8, 2015 #4
    I moved the close() function around at the end of the program and I still get the same message :

    At line 32 of file PRIME.f90 (unit = 2, file = 'fort.2')
    Fortran runtime error: End of file
     
  6. Oct 8, 2015 #5

    Mark44

    Staff: Mentor

    What does your code look like now? The error message seems to indicate that you're working with unit 2, but your code is opening unit 1. By the way, you need to specify that you want to write to the file, something like this:
    Code (Text):
    open (1, file="MY_PRIME.out", action="write")
    Also, it would be good to hand-simulate what your code is doing for a few iterations. You have primes(1) set to 2. Your code should set primes(2) to 3, and primes(3) to 5.
     
  7. Oct 8, 2015 #6

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    Try these changes:
    1) Don't use small numbers like 1 for the unit number of the output file. They usually have special meanings. Try something like 10 or larger.
    2) open the file specifically for writing. (ACTION='WRITE')
     
  8. Oct 8, 2015 #7
    I did try 10, I do not think it is reading/writing for some reason
     
  9. Oct 8, 2015 #8

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    Try opening it specifically for write, ACTION='WRITE'.
     
  10. Oct 8, 2015 #9

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    The error message is referring to a different problem. It is trying to use unit 2, and file fort.2. Could that be the unit 'print' needs in line 31?. It may be the monitor and the batch file can't use it. Try some minimal simple programs just to test print and write.
     
  11. Oct 8, 2015 #10
    I made some adjustments:
    Sues-MacBook-Air:FORTRAN sueparks$ gfortran prime.f90
    Sues-MacBook-Air:FORTRAN sueparks$ ./MY_PRIME.out

    ./MY_PRIME.out: line 1: THE: command not found
    ./MY_PRIME.out: line 2: THE: command not found
    ./MY_PRIME.out: line 3: THE: command not found
    ./MY_PRIME.out: line 4: THE: command not found
    ./MY_PRIME.out: line 5: THE: command not found
    ./MY_PRIME.out: line 6: THE: command not found
    ./MY_PRIME.out: line 7: THE: command not found ... etc

    Code (Fortran):

        PROGRAM PRIME

        implicit none

        integer, PARAMETER  ::  M = 100
        integer :: at, found, i
        logical :: is_prime
        integer, dimension(M) :: primes ! array that will hold the primes
     
        open (11, file = "MY_PRIME.OUT", ACTION="WRITE")

    !print *, "How many primes would you like to find?"
    !read *,  num_primes

        primes(1) = 2
        at = 2
        found = 1
        do
            is_prime = .true. ! assume prime
            do i = 1, found
                if (modulo(at, primes(i)) == 0) then ! if divisible by any other element
                    is_prime = .false.               ! in the array, then not prime.
                    exit
                end if
            end do
            if (is_prime) then
                found = found + 1
                primes(found) = at
                print *, at
                write (11,*) at
             
             
           
             
            end if
            at = at + 1
         
            if (found == M) then ! stop when all primes are found
                exit
            end if
         close(11)
        end do
     

        END PROGRAM PRIME
     
     
    Last edited by a moderator: Oct 8, 2015
  12. Oct 8, 2015 #11
    I think I got it, I believe it was my commands:

    Sues-MacBook-Air:FORTRAN sueparks$ gfortran prime.f90 -o MY_PRIME.out
    Sues-MacBook-Air:FORTRAN sueparks$ ./MY_PRIME.out

    3
    5
    7
    11
    13
    17
    19
    23
    29
    31
    37
    41
    43

    P.S. is this how an array would print?
     
  13. Oct 8, 2015 #12

    Mark44

    Staff: Mentor

    Your program should print 2 as the first prime. You probably have an "off by one error" AKA OBOE.
     
  14. Oct 8, 2015 #13

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    Before you call this solved, there is something you should think about. I think that your print is printing to MY_PRIME.out because of the -o option and the write is also trying to write to MY_PRIME.out because of the open statement. I don't know how that is working. It may be just luck. You should put your prints to one file and write to another file.
     
  15. Oct 8, 2015 #14

    Mark44

    Staff: Mentor

    I don't think this is what's happening. The -o option specifies the name of the object file produced, in this case, the executable. It doesn't have anything to do with what happens at runtime.

    The print statement defaults to sending output to the standard output device. The write(11, ...) statement sends output to the file specified in the associated open statement.
    Her intent, I believe, is to use print to send output to the monitor and write to send output to the file.
     
  16. Oct 8, 2015 #15
    It did both, it printed to the console and it filled the text file. They both look the same.
     
  17. Oct 8, 2015 #16
    So, it printed everything but 2. Let me check my loops and variables again.
     
  18. Oct 9, 2015 #17

    Mark44

    Staff: Mentor

    As I said in post #5,
    Since you know the size of your array, you would be better off using an iterative loop (DO I = 1, N) than the plain DO loop you have as your outer loop. At any rate, check the value of your array index for the first number you print/write.
     
  19. Oct 9, 2015 #18

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    Oh, that's right. But then the write statement must be overwriting the object file. I am sure that is not what you want to do.
     
  20. Oct 9, 2015 #19
    Back in post #10, you compile your program for the n-th time with "gfortran prime.f90"...this produces an executable with defualt name of "a.out"; BUT, your attempted to run an executable of name "MY_PRIME.out"...which was produced in one of your previous runs and filled with lines like "THE FIRST 100..." and so, that is why the computer complains of "command not found" because "THE" is not a command.

    Then, when you started to compile with ' -o ', you made the bad choice of requesting and executable with the same name as the file you chose to write from the program itself "MY_PRIME.out"...so, yes, you are replacing your program executable with a text file. The reason why this works is simpy because in order to run the program, the computer first loads it into memory, once there, it can be deleted from disk or over written, not a problem.

    The reason why the 2 does not show up is because you assign that one before you enter the loop; but,the loop only prints prime numbers found during the loop itself, not before.
     
  21. Oct 10, 2015 #20

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    @Sue Parks , You should get into the habit of using file extensions that tell you what type of file it is. Typically, the output of the compile and built should have a .EXE extension to tell you that it is an executable file ( -o MY_PRIME.EXE ). Readable text from the write statement should have a .TXT extension ( open (11, file="MY_PRIME.TXT", ACTION="WRITE") ).
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Fortran (90) runtime error: End of file
  1. [FORTRAN] Runtime error (Replies: 12)

Loading...