Fortran Fortran (90) runtime error: End of file

AI Thread Summary
The discussion revolves around a Fortran 90 program designed to find prime numbers between 1 and 100. Users encountered a runtime error related to file handling, specifically an "End of file" error, which was traced back to incorrect file unit management and premature file closure. Suggestions included using larger unit numbers, ensuring the file is opened for writing, and avoiding naming conflicts between the executable and output files. The final solution involved correctly structuring the program to print primes to the console and write them to a separate output file without overwriting the executable. The conversation emphasizes best practices in file management and debugging in Fortran programming.
Sue Parks
Messages
38
Reaction score
0
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

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:
Technology news on Phys.org
Sue Parks said:
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).
?
It looks like you didn't finish the last sentence.
 
You are closing the output file as soon as you find any prime, so you'll just print one prime in your output file.
 
  • Like
Likes Sue Parks
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
 
Sue Parks said:
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
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:
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.
 
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')
 
I did try 10, I do not think it is reading/writing for some reason
 
Sue Parks said:
I did try 10, I do not think it is reading/writing for some reason
Try opening it specifically for write, ACTION='WRITE'.
 
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.
 
  • #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

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:
  • #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?
 
  • #12
Sue Parks said:
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?
Your program should print 2 as the first prime. You probably have an "off by one error" AKA OBOE.
 
  • #13
Sue Parks said:
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
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.
 
  • Like
Likes Sue Parks
  • #14
FactChecker said:
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 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.
FactChecker said:
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.
Her intent, I believe, is to use print to send output to the monitor and write to send output to the file.
 
  • Like
Likes Sue Parks
  • #15
It did both, it printed to the console and it filled the text file. They both look the same.
 
  • #16
So, it printed everything but 2. Let me check my loops and variables again.
 
  • #17
As I said in post #5,
Mark44 said:
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.

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.
 
  • #18
Mark44 said:
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.
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.
 
  • #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.
 
  • #20
@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") ).
 

Similar threads

Replies
5
Views
5K
Replies
12
Views
3K
Replies
5
Views
2K
Replies
3
Views
3K
Replies
2
Views
9K
Replies
22
Views
5K
Replies
16
Views
2K
Replies
2
Views
25K
Back
Top