Reputation: 425
I am learning about Fortran and currently doing the exercise on fortrantutorials.com. I have to run the following code:
program magic
implicit none
real, dimension(100) :: a,b,c,d
open(10, file='data.txt')
read(10,*) a
b = a*10
c = b-a
d = 1
print*, 'a = ', a
print*, 'b = ', b
print*, 'c = ', c
print*, 'd = ', d
end program magic
It reads the following data.txt file:
24
45
67
89
12
99
33
68
37
11
When I run it, it shows this error:
At line 6 of file test.f95 (unit = 10, file = 'data.txt')
Fortran runtime error: End of file
[Finished in 0.0s with exit code 2]
Line 6 refers to the following line, and I have double checked that the 'data.txt' and my fortran file are indeed in the same directory:
read(10,*) a
What can I do to resolve this problem? Thanks in advance.
Upvotes: 0
Views: 1505
Reputation: 6158
If you add IOSTAT=<scalar-int-variable>
to your read, it will set that variable instead of crashing:
integer :: IOSTAT
CHARACTER*(128) :: IOMSG
open(10, file='data.txt')
read(10,*,IOSTAT=IOSTAT,IOMSG=IOMSG) a
IF ( IOSTAT .NE. 0 ) THEN
WRITE(*,*) "WARNING: Read failed with message '", TRIM(IOMSG), "'"
END IF
Do not trust the results of such a failed READ statement.
Upvotes: 1
Reputation: 59999
read(10,*) a
tries to read 100 numbers, because size of a
is 100
real, dimension(100) :: a
Your file does not contain 100 numbers, so it crashes when it reaches the end of the file.
Just read the message the compiler tells you:
"Fortran runtime error: End of file"
Upvotes: 1