Fortran Runtime error while reading file in fortran90

AI Thread Summary
The discussion centers around a Fortran coding issue related to reading a triangular matrix from a file named "data.dat." The user encounters a runtime error indicating "End of file" due to the way the READ statement is structured. The solution involves using an IOSTAT specifier to handle end-of-file conditions and modifying the READ statement to read an entire row of the matrix at once, rather than one element at a time. This can be achieved by eliminating the inner loop and using an implied DO loop in the READ statement. A suggestion is made to include WRITE statements for debugging purposes to verify that data is being read correctly. The user confirms that the provided solution resolves the issue.
kranthi4689
Messages
16
Reaction score
0
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.
 
Technology news on Phys.org
Use an IOSTAT specifier in your READ statement to tell the program what to do when it reaches the end of file.

https://pages.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/iostatus.html
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...".)
 
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)
 
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.
 
jtbell said:
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.
 
You're welcome :wink:
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
17
Views
6K
Replies
1
Views
3K
Replies
8
Views
1K
Replies
22
Views
5K
Replies
12
Views
3K
Replies
16
Views
3K
Replies
5
Views
2K
Replies
1
Views
2K
Back
Top