Fortran (90) runtime error: End of file

In summary: WRITE (1, *)"THE FIRST 100 PRIME NUMBERS:" ,AT CLOSE (1) at = at +1 if (found == M) then ! stop when all primes are found
  • #1
Sue Parks
38
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
  • #2
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.
 
  • #3
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
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
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.
 
  • #6
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
I did try 10, I do not think it is reading/writing for some reason
 
  • #8
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'.
 
  • #9
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") ).
 

1. What does the "Fortran (90) runtime error: End of file" mean?

The "Fortran (90) runtime error: End of file" is an error message that appears when a Fortran program reaches the end of a file without finding the expected data. This usually occurs when the program is trying to read data from a file that does not exist or has been closed prematurely.

2. How can I fix the "Fortran (90) runtime error: End of file"?

To fix this error, you will need to check your code to ensure that the file you are trying to read from exists and is in the correct format. You should also make sure that the file is not being closed prematurely. If the error persists, you can try using error handling techniques such as "I/O error trapping" to handle the error and continue the program execution.

3. Is the "Fortran (90) runtime error: End of file" a common error?

Yes, the "Fortran (90) runtime error: End of file" is a common error that can occur when working with files in Fortran. It is important to properly handle this error in your code to prevent unexpected program crashes.

4. Can this error occur for other reasons besides reaching the end of a file?

No, this error specifically occurs when the program reaches the end of a file without finding the expected data. If your program is not working properly and you are not receiving this error, it is likely caused by another issue.

5. How can I prevent the "Fortran (90) runtime error: End of file" from occurring?

To prevent this error, you should make sure to properly handle file input/output operations in your code. This includes checking if the file exists, is in the correct format, and is not being closed prematurely. You can also use error handling techniques to handle this error and continue the program execution.

Similar threads

  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
607
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
8K
  • Programming and Computer Science
Replies
22
Views
4K
Replies
11
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
Back
Top