Hello,
I'm just learning Fortran and I haven't been able to solve this simple problem after searching through various Fortran books. I have a file called test1.txt with the following data:
1.0 10
2.0 20
3.0 30
4.0 40
5.0 50
6.0 40
7.0 30
8.0 20
9.0 10
I'm just playing with commands and all I want to do is read ALL of the data into two arrays and then work with the data. The problem is, it doesn't read the last line for me. Here is the test code:
Program test1
integer count
real sum, time, temp, ave
open (unit=10,file='test1.txt',status='old')
sum=0.0
count=0
5 read (10,*,END=15) time, temp
sum=sum+temp
count=count+1
print*, sum
print*, count
go to 5
15 if (count.EQ.0) then
print*, 'no data according to record count'
else
ave=sum/real(count)
print 25, count, ave
25 format(1x,'count=',I3,5X,'average=',F8.2,'degrees f')
end if
end
And the output looks like this:
10.0
1
30.0
2
60.0
3
100.0
4
150.0
5
190.0
6
220.0
7
240.0
8
count = 9 average = 30.00 degrees f
You'll notice that it should say count=9 and average = 27.78
I've tried the three most common ways of input from a file like using a do loop if I know exactly how many values are in the file, using a trailer signal by placing something like 999 at the end of the file, and using the END statement like in my code currently. All of them produced the same results.
I'm using Visual Fortran 5.0 with Fortran 90
Any suggestions?
Thank you,
Mike
I'm just learning Fortran and I haven't been able to solve this simple problem after searching through various Fortran books. I have a file called test1.txt with the following data:
1.0 10
2.0 20
3.0 30
4.0 40
5.0 50
6.0 40
7.0 30
8.0 20
9.0 10
I'm just playing with commands and all I want to do is read ALL of the data into two arrays and then work with the data. The problem is, it doesn't read the last line for me. Here is the test code:
Program test1
integer count
real sum, time, temp, ave
open (unit=10,file='test1.txt',status='old')
sum=0.0
count=0
5 read (10,*,END=15) time, temp
sum=sum+temp
count=count+1
print*, sum
print*, count
go to 5
15 if (count.EQ.0) then
print*, 'no data according to record count'
else
ave=sum/real(count)
print 25, count, ave
25 format(1x,'count=',I3,5X,'average=',F8.2,'degrees f')
end if
end
And the output looks like this:
10.0
1
30.0
2
60.0
3
100.0
4
150.0
5
190.0
6
220.0
7
240.0
8
count = 9 average = 30.00 degrees f
You'll notice that it should say count=9 and average = 27.78
I've tried the three most common ways of input from a file like using a do loop if I know exactly how many values are in the file, using a trailer signal by placing something like 999 at the end of the file, and using the END statement like in my code currently. All of them produced the same results.
I'm using Visual Fortran 5.0 with Fortran 90
Any suggestions?
Thank you,
Mike