Fortran runtime error: End of file

In summary, the conversation was about a program to read data from a file and write it to a new file. The program encountered an error due to the last line of the file having only two values instead of six. The solution was to use the End=999 option on the READ statement. After implementing the solution, the program ran successfully.
  • #1
marlh
12
0
Dear all,
I want to read file "d1.dat":

1.025000+7 5.794453+0 1.050000+7 5.770080+0 1.075000+7 5.750135+0
1.100000+7 5.734860+0 1.125000+7 5.724448+0 1.150000+7 5.719060+0
1.175000+7 5.718829+0 1.200000+7 5.723865+0 1.219440+7 5.730802+0
1.225000+7 5.732945+0 1.250000+7 5.743325+0 1.275000+7 5.754911+0
1.300000+7 5.767697+0 1.325000+7 5.781676+0 1.350000+7 5.796840+0
1.375000+7 5.813182+0 1.400000+7 5.830690+0 1.425000+7 5.846650+0
1.450000+7 5.862611+0 1.475000+7 5.878547+0 1.500000+7 5.894463+0
1.525000+7 5.910365+0 1.550000+7 5.926258+0 1.575000+7 5.942145+0
1.600000+7 5.958034+0 1.650000+7 5.999165+0 1.700000+7 6.044866+0
1.750000+7 6.088568+0 1.796240+7 6.122193+0 1.800000+7 6.124515+0
1.850000+7 6.147738+0 1.900000+7 6.159560+0 1.950000+7 6.165319+0
2.000000+7 6.169678+0

then, i write data to a new file "newd1.dat":

1.025000+7 5.794453+0
1.050000+7 5.770080+0
1.075000+7 5.750135+0
1.100000+7 5.734860+0
1.125000+7 5.724448+0
1.150000+7 5.719060+0
1.175000+7 5.718829+0
1.200000+7 5.723865+0
1.219440+7 5.730802+0
1.225000+7 5.732945+0
1.250000+7 5.743325+0
1.275000+7 5.754911+0
1.300000+7 5.767697+0
1.325000+7 5.781676+0
1.350000+7 5.796840+0
1.375000+7 5.813182+0
1.400000+7 5.830690+0
1.425000+7 5.846650+0
1.450000+7 5.862611+0
1.475000+7 5.878547+0
1.500000+7 5.894463+0
1.525000+7 5.910365+0
1.550000+7 5.926258+0
1.575000+7 5.942145+0
1.600000+7 5.958034+0
1.650000+7 5.999165+0
1.700000+7 6.044866+0
1.750000+7 6.088568+0
1.796240+7 6.122193+0
1.800000+7 6.124515+0
1.850000+7 6.147738+0
1.900000+7 6.159560+0
1.950000+7 6.165319+0
2.000000+7 6.169678+0

my code:
!*********************************************
program readdata

implicit none
integer :: i,k
REAL :: x(6)

integer:: n
character(len=60):: cmd
cmd = "cat d1.dat | grep '[^ ]' | wc -l > nlines.txt"
call system(cmd)

open(1,file='nlines.txt')
read(1,*) n
print*, "Number of lines are", n
cmd = 'rm nlines.txt'
call system(cmd)

open(10,file="d1.dat",status="old")
open(11,file="newd1.dat")

do i = 1,n

read(10,*) x

write(11,99) x(1:2)
write(11,99) x(3:4)
write(11,99) x(5:6)

99 format(e12.7,1x,e12.7)
end do
close(10)
close(11)


end program readdata
!********************************************
compiler is ok by gfortran, but when i run, program has error:

" At line 23 of file readdata.f90 (unit=10,file='d1.dat')
Fortran runtime error: End of file"

Please help me,

Thanks!
 
Technology news on Phys.org
  • #3
Dear jedishrfu,

Thanks for your help. I done success. :)
 

FAQ: Fortran runtime error: End of file

What is a "Fortran runtime error: End of file"?

A "Fortran runtime error: End of file" is an error message that indicates the program has tried to read beyond the end of a file, meaning there is no more data left to be read. This can occur when the program is expecting more data but reaches the end of the file before it is able to retrieve it.

What causes a "Fortran runtime error: End of file"?

There are a few potential causes for a "Fortran runtime error: End of file". One common cause is when the program tries to read data from a file that has already been fully read. Another cause could be a mismatch between the expected format of the data and the actual format in the file. Additionally, errors in the code such as a missing or misplaced loop can also lead to this error.

How can I fix a "Fortran runtime error: End of file"?

The best way to fix a "Fortran runtime error: End of file" is to carefully review your code and identify where the issue is occurring. You can then make any necessary adjustments, such as adding a loop to continue reading until the end of the file is reached, or ensuring the format of the data matches what is expected by the program. It may also be helpful to use debugging tools to track the flow of the program and identify the source of the error.

Can I prevent a "Fortran runtime error: End of file" from occurring?

While it is not always possible to prevent a "Fortran runtime error: End of file" from occurring, there are steps you can take to reduce the likelihood of encountering this error. This includes carefully designing and testing your code, ensuring the format of your data matches what is expected, and implementing error-handling procedures to catch and handle any potential issues.

Are there any resources available for troubleshooting "Fortran runtime error: End of file"?

Yes, there are a variety of resources available for troubleshooting "Fortran runtime error: End of file". These include online forums and communities where experienced Fortran programmers can offer advice and guidance, as well as documentation and tutorials provided by the Fortran language itself. Additionally, consulting with other scientists and researchers who have experience with Fortran may also be helpful in resolving this error.

Similar threads

Replies
12
Views
2K
Replies
4
Views
2K
Replies
16
Views
3K
Replies
2
Views
8K
Replies
5
Views
4K
Replies
17
Views
5K
Replies
5
Views
2K
Replies
3
Views
2K
Back
Top