Runtime error while reading file in fortran90

In summary, the code is reading the data in a file, but is getting an error when it reaches the end of the file. The error is most likely that the data is not being read on a single line. The solution is to read all of the data on a line in a single call to READ.
  • #1
kranthi4689
16
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
  • #2
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...".)
 
  • #3
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)
 
  • #4
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.
 
  • #5
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.
 
  • #6
You're welcome :wink:
 

1. What is a runtime error while reading file in Fortran90?

A runtime error while reading file in Fortran90 is an error that occurs during the execution of a program, specifically while trying to read data from a file. This error can be caused by a variety of factors, such as incorrect file paths or formatting issues.

2. How can I fix a runtime error while reading file in Fortran90?

To fix a runtime error while reading file in Fortran90, you will need to identify the cause of the error. This could involve checking the file paths and making sure they are correct, ensuring that the file is formatted correctly, or using debugging tools to track down the source of the error in your code.

3. Why am I getting a runtime error while reading file in Fortran90?

There are many reasons why you might be getting a runtime error while reading file in Fortran90. Some common causes include invalid file paths, incorrect file formatting, or errors in your code that are preventing the file from being read properly.

4. How can I prevent a runtime error while reading file in Fortran90?

To prevent a runtime error while reading file in Fortran90, it is important to carefully check and validate all file paths and ensure that your file is properly formatted. Additionally, using good programming practices such as proper error handling and debugging can also help prevent these errors.

5. Can external factors cause a runtime error while reading file in Fortran90?

Yes, external factors such as changes in the file system or hardware failures can also contribute to a runtime error while reading file in Fortran90. It is important to regularly check and maintain your system to avoid these types of errors.

Similar threads

  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
4
Views
565
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
7K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top