# Fortran (90) runtime error: End of file

• Fortran

## Main Question or Discussion Point

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

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:

Related Programming and Computer Science News on Phys.org
Mark44
Mentor
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.

phinds
Gold Member
2019 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.

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

Mark44
Mentor
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.

FactChecker
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')

I did try 10, I do not think it is reading/writing for some reason

FactChecker
Gold Member
I did try 10, I do not think it is reading/writing for some reason
Try opening it specifically for write, ACTION='WRITE'.

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

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

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

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

Mark44
Mentor
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.

FactChecker
Gold Member
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.

Sue Parks
Mark44
Mentor
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.

Sue Parks
It did both, it printed to the console and it filled the text file. They both look the same.

So, it printed everything but 2. Let me check my loops and variables again.

Mark44
Mentor
As I said in post #5,
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.

FactChecker
Gold Member
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.

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.

FactChecker