Fortran, File Read Error( Tried to read past )

  • Context: Fortran 
  • Thread starter Thread starter Ordain
  • Start date Start date
  • Tags Tags
    Error File Fortran
Click For Summary

Discussion Overview

The discussion revolves around a Fortran programming issue related to reading data from a file and encountering a "attempted to read past end of file" error. Participants explore potential causes and solutions for this error within the context of file input/output operations in Fortran.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their Fortran program and the error encountered when reading a data file, specifying the structure of the data file.
  • Another participant suggests changing the format string in the READ statement to '2F8.1' to correctly read both x and y values.
  • A different participant confirms that N was less than forty and mentions modifying the format string to standard '*' which resolved the issue, implying that the original format may have caused the y values to be ignored.
  • One participant recalls that using a single format string can lead to repeated application of the format, potentially causing issues with reading values correctly.
  • Another participant recommends avoiding format strings altogether in the READ statement, advocating for a list-oriented approach to prevent difficulties with varying data formats.

Areas of Agreement / Disagreement

Participants express differing opinions on the use of format strings in Fortran's READ statements, with some advocating for their use while others suggest avoiding them. The discussion does not reach a consensus on the best approach.

Contextual Notes

Participants note potential limitations related to the format of the data being read and the implications of using specific format strings, but do not resolve these issues definitively.

Ordain
Messages
5
Reaction score
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


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

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


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.
 


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.
 


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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
16K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K