Fortran Fortran runtime error: End of file

AI Thread Summary
The user encountered a Fortran runtime error stating "End of file" while trying to read from the file "d1.dat" using a program that expects to read six values at a time. The issue arose because the last line of the input file contains only two values, leading to an attempt to read beyond the available data. Suggestions were made to handle this situation by checking the number of values read or using the End=999 option in the READ statement. After implementing the suggested changes, the user successfully resolved the issue. The discussion highlights the importance of ensuring data consistency in file reading operations.
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. :)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

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