Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29361 Discussions

Simple question reagarding Fortran input from a file

uconn2323
Beginner
767 Views
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 code here
your text hereyour text hereyour text here
0 Kudos
2 Replies
Steven_L_Intel1
Employee
767 Views
My guess is that your data file does not end with a newline - the first release of DVF would not read records that didn't end with a newline. You should be able to correct this by downloading and installing the 5.0D update from the Compaq web site, or just going into the data file in NOTEPAD, goint to the end of the last line, and pressing Enter.

Steve
0 Kudos
uconn2323
Beginner
767 Views
Steve,
Thank you so much! What a simple fix! Version 5.0D read the file with no problem and I did not need to enter a new line in my file.

Thank you very much for your fast and efficient help.

-Mike
0 Kudos
Reply