- #1
Ordain
- 5
- 0
Fortran, File Read Error[Resolved itself, fixed]
Basically I'm writing a simple fortran program to read in a data file formatted as shown below and then calculate the line of best fit for the given data points.
N
x1 y2
x2 y2
. .
. .
. .
xn yn
Where N is the number of rows in the data file. The code is shown below, compiles fine but when I run the program and input the data file name say, "info.dat" I get a "attempted to read past end of file" error at line 21, which is where my DO loop is, but the do loop is only from 1 to N, so what could be the issue?
Thanks Fellas.
Basically I'm writing a simple fortran program to read in a data file formatted as shown below and then calculate the line of best fit for the given data points.
N
x1 y2
x2 y2
. .
. .
. .
xn yn
Where N is the number of rows in the data file. The code is shown below, compiles fine but when I run the program and input the data file name say, "info.dat" I get a "attempted to read past end of file" error at line 21, which is where my DO loop is, but the do loop is only from 1 to N, so what could be the issue?
Code:
PROGRAM LineOfBestFit
IMPLICIT NONE
REAL:: xavg,yavg, sumx, sumxy,sumxx,sumy,slope,intercept
REAL,DIMENSION(40)::x, y
INTEGER::N,i, iostat,openstatus !N is the number of points which will be first line of data file
CHARACTER::filename*20
sumx=0
sumxy=0
sumxx=0
sumy=0
[B]PRINT*,[/B] "Enter the name of the data file which contains the data to be evaluated."
[B]READ*,[/B] filename
[B]OPEN[/B](UNIT=1,FILE=filename,STATUS='OLD',IOSTAT=OpenStatus)
!Check for successful open.
[B]IF[/B](OpenStatus>0)[B]THEN[/B]
PRINT*, "Unable to open file!"
[B]STOP[/B]
[B]ENDIF[/B]
[B]READ[/B](1,*)N
[B]DO[/B] i=1,N
[B] READ[/B](1,FMT='(F8.1)')x(i),y(i)
sumx=sumx+x(i)
sumy=sumy+y(i)
sumxy=sumxy+x(i)*y(i)
sumxx=sumxx+x(i)**2
[B]ENDDO[/B]
CLOSE(1)!Done with file, so close it.
xavg=sumx/N
yavg=sumy/N
slope=(sumxy-sumx*yavg)/(sumxx-sumx*xavg)
intercept=yavg-slope*xavg
WRITE(*,10) "The line of best fit is: y=",slope,"x+",intercept
10 FORMAT(A,F4.2,A,F4.2)
[B]ENDPROGRAM[/B] LineOfBestFit
Thanks Fellas.
Last edited: