Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Very easy question regarding Fortran input

Status
Not open for further replies.

uconn2323

Programmer
Mar 12, 2003
1
US
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
 
Your program worked fine for me, using Visual Fortran 6.5. I suspect there is something wrong with your data file. Try cutting and pasting the data off your posting into another file and rerunning. CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Not sure how your output produced "count = 9". Was that a type-o and should have been 8? In any case, edit you input file and go to the very end. Is the cursor at the end of the last line, or underneath the last line? If it is at the end of the line, the program might be reading the "end of file" character along with the last line of the file, which will make it jump to line 15 (from the END= phrase) and not process the data on the last line. If this is the case, edit the input file, go to the end of the last line, hit "enter" (carriage control, line feed), then save the file. Rerun the program to see if that fixes the problem.
 
Do you need a newline at the end of data? i.e.

9.0 10<NL>
<EOF>

not

9.0 10<EOF>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top