Fortran, File Read Error( Tried to read past )

In summary, the programmer was having trouble reading in the data file because the data wasn't in the correct format. They replaced the f8.1 with *. This resolved the issue.
  • #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?

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:
Technology news on Phys.org
  • #2


Try making your format string FMT = '2F8.1' when reading your x and y values.

Also make sure N <= 40 in the data file.
 
  • #3


SteamKing said:
Try making your format string FMT = '2F8.1' when reading your x and y values.

Also make sure N <= 40 in the data file.

N was definitely less than forty in the data file, I tinkered around with the code a bit and replaced the f8.1 with the standard *. Seemed to resolve the problem, I imagine the issue was that I only read the leftmost column and skipped the right column.
 
  • #4


I'm going on memory, but when you use a single format enclosed in parenthesis, like '(F8.1)', FORTRAN will keep using this format, but after reading one value, the format is repeated only after a new record (or new line) is issued. So, in your program, you would have read a sequence of x values, but the y values would be ignored.
 
  • #5


The best thing to do, here, is to NOT use a format string in your READ statement and just let it be (1,*) ...this is what you did when reading the integer number...

If you specify a format, then, if the numbers are not exactly like that, you may have difficulties...just leave it (n,*)...this is called "list oriented" and it is the only thing I ever use...for reading, that is.
 

1. What does the error "Fortran, File Read Error (Tried to read past )" mean?

This error occurs when a program attempts to read data from a file, but the end of the file has been reached before all of the data has been read. This can happen if the file is shorter than expected or if there is an error in the way the data is being read.

2. How can I fix this error in my Fortran code?

In order to fix this error, you will need to check your code to ensure that the file is being read correctly and that all of the data is being processed. You may also need to check the file itself to make sure it contains the expected data and is not corrupted in any way. If necessary, you can also use error handling techniques to catch this error and handle it appropriately in your code.

3. Is this error specific to Fortran or can it occur in other programming languages?

This error is specific to Fortran and is caused by the way the language handles file input and output. Other programming languages may have similar errors, but they will likely be named differently and have different causes.

4. Can this error be prevented?

Yes, this error can be prevented by thoroughly testing your code and ensuring that all file input and output is handled correctly. You can also use error handling techniques to catch and handle this error in your code.

5. What other common errors are associated with Fortran file input and output?

Other common errors associated with Fortran file input and output include "File Open Error" (when a file cannot be opened), "File Write Error" (when there is an error writing to a file), and "End of File Error" (when the program reaches the end of a file unexpectedly).

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
741
  • Programming and Computer Science
Replies
4
Views
607
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top