Fortran runtime error: End of file

Click For Summary
SUMMARY

The forum discussion addresses a Fortran runtime error encountered when attempting to read from a file named "d1.dat". The error occurs because the program attempts to read six values at a time, while the last line of the file contains only two values. The user successfully resolves the issue by implementing the END=999 option in the READ statement, which allows the program to handle the end of the file gracefully. The compiler used is gfortran, and the solution references additional resources for handling I/O status in Fortran.

PREREQUISITES
  • Understanding of Fortran programming language
  • Familiarity with file I/O operations in Fortran
  • Knowledge of gfortran compiler usage
  • Basic understanding of error handling in programming
NEXT STEPS
  • Learn about Fortran I/O status handling using the END= option
  • Explore advanced file reading techniques in Fortran
  • Investigate error handling best practices in Fortran programming
  • Review the documentation for gfortran to understand its features and limitations
USEFUL FOR

This discussion is beneficial for Fortran developers, programmers troubleshooting file I/O issues, and anyone looking to enhance their understanding of error handling in Fortran applications.

marlh
Messages
12
Reaction score
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
Dear jedishrfu,

Thanks for your help. I done success. :)
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
12
Views
9K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 17 ·
Replies
17
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K