Fortran (90) runtime error: End of file

  • Fortran
  • Thread starter Sue Parks
  • Start date
  • #1
38
0

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

Answers and Replies

  • #2
33,646
5,315
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.
 
  • #3
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
16,189
6,197
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
  • #4
38
0
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
 
  • #5
33,646
5,315
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.
 
  • #6
FactChecker
Science Advisor
Gold Member
5,591
2,074
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')
 
  • #7
38
0
I did try 10, I do not think it is reading/writing for some reason
 
  • #8
FactChecker
Science Advisor
Gold Member
5,591
2,074
I did try 10, I do not think it is reading/writing for some reason
Try opening it specifically for write, ACTION='WRITE'.
 
  • #9
FactChecker
Science Advisor
Gold Member
5,591
2,074
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
38
0
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
38
0
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
33,646
5,315
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
FactChecker
Science Advisor
Gold Member
5,591
2,074
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
33,646
5,315
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
38
0
It did both, it printed to the console and it filled the text file. They both look the same.
 
  • #16
38
0
So, it printed everything but 2. Let me check my loops and variables again.
 
  • #17
33,646
5,315
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.
 
  • #18
FactChecker
Science Advisor
Gold Member
5,591
2,074
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
1,065
53
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
FactChecker
Science Advisor
Gold Member
5,591
2,074
@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") ).
 

Related Threads on Fortran (90) runtime error: End of file

Replies
2
Views
19K
Replies
2
Views
7K
Replies
5
Views
941
  • Last Post
Replies
17
Views
839
  • Last Post
Replies
12
Views
5K
Replies
4
Views
8K
  • Last Post
Replies
13
Views
6K
  • Last Post
Replies
7
Views
7K
Replies
5
Views
5K
Top