Fortran Runtime error while reading file in fortran90

I'm new to fortran coding and trying to read a file called "data.dat" with data like a triangular matrix
0.1
0.1 0.2
0.1 0.2 0.3
0.1 0.2 0.3 0.4
0.1 0.2 0.3 0.4 0.5


using the code

PROGRAM matrix
IMPLICIT NONE
REAL, DIMENSION(5,5) :: a
INTEGER :: row,col,max_rows,max_cols
max_rows=5
max_cols=5
OPEN(UNIT=10, FILE="dat.dat",status='old')
DO row = 1,max_rows
DO col = 1,max_cols
if (col .le. row) then
read(10,*) a(row,col)
else
a(row,col) = 0.0
endif
END DO
END DO
DO row = 1,max_rows
WRITE(*,*) (a(row,col),col = 1,max_cols)
END DO
END PROGRAM matrix


While executing the code I get the following error
At line 11 of file matrix.f90 (unit = 10, file = 'dat.dat')
Fortran runtime error: End of file
Error termination. Backtrace:


How do I rectify this error. kinldy help me.
 

jtbell

Mentor
15,285
2,870
Use an IOSTAT specifier in your READ statement to tell the program what to do when it reaches the end of file.


Aha, now I see something else which is likely your real problem. You're reading only one item at a time. Each READ statement reads a whole line at once, so this requires that each item be on a separate line.

If each line of the file contains a whole row of the matrix, the READ statement should read all of those items at once.

(it looks like BvU wrote his post while I was adding the "Aha...".)
 

BvU

Science Advisor
Homework Helper
11,810
2,571
Hi,

Your read statement starts on a new record (line) for every call, so after five calls it hits end-of file.

You need to read all values on a line in a single call to read, e.g. with an implied DO loop .
So get rid of the DO col = 1,max_cols and use something like (I didn't try it out)

Fortran:
read (10,*) (a(row,col), col = 1, row)
 

jtbell

Mentor
15,285
2,870
There should be one more left-parenthesis:

Code:
read (10,*) (a(row,col), col = 1, row))
Tip: for testing and debugging, always put a WRITE statement immediately after every new READ statement, so you can check that you're actually reading what you intended to read. After you're sure your READ is working properly, you can remove the WRITE.
 
There should be one more left-parenthesis:

Code:
read (10,*) (a(row,col), col = 1, row))
Tip: for testing and debugging, always put a WRITE statement immediately after every new READ statement, so you can check that you're actually reading what you intended to read. After you're sure your READ is working properly, you can remove the WRITE.
thank you @jtbell.. it worked.
 

BvU

Science Advisor
Homework Helper
11,810
2,571
You're welcome :wink:
 

Want to reply to this thread?

"Runtime error while reading file in fortran90" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top