Fortran Reading and Displaying text file in Fortran

AI Thread Summary
The discussion revolves around troubleshooting a Fortran 90 program designed to read and display data from a text file. The initial code faced an "end of file" runtime error, prompting the user to seek help. Key issues identified included a space in the variable name "File Name" and an incorrect FORMAT statement that did not match the data structure of the input file. Suggestions included using a DO loop to read multiple lines and ensuring the input format aligns with the data's structure. The user experimented with different FORMAT statements and input methods, ultimately resolving the issue by correcting the FORMAT statement and ensuring the data was read correctly. The final code successfully displayed the data, indicating that the program was functioning as intended.
Azorio
Messages
6
Reaction score
0
I am trying to read and display the content of a text file in Fotran 90 to make sure that fortran is interpreting the data correctly. Eventually I want to store the data in an array so I can calculate averages, but at the moment I am still working on getting the data displayed in Fortran. I've been at it for a couple hours and cannot figure out the problem. I am getting an end of the file runtime error. Here is the code:

PROGRAM testx
IMPLICIT NONE
CHARACTER(20) :: File Name
INTEGER :: Count = 0, OpenStatus, InputStatus
REAL :: Y, M, D, Rain, Tmax, Tmin, srain, stmax, stmin
WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter name of data file:"
READ *, FileName
OPEN (UNIT = 15, FILE = FileName, STATUS = "OLD")
READ(15,*) Y, M, D, Rain, Tmax, Tmin
100 FORMAT (6x)
PRINT 100, "Year", "Month", "Day", "Rainfall", "MxTemp", "MiTemp"
END PROGRAM testx

Here is the text file containing the data I am working with:
View attachment testx.txt
 
Last edited:
Technology news on Phys.org
When you enter code in your post, please surround it with [noparse]
Code:
 and
[/noparse] tags. I have done this below.
Azorio said:
I am trying to read and display the content of a text file in Fotran 90 to make sure that fortran is interpreting the data correctly. Eventually I want to store the data in an array so I can calculate averages, but at the moment I am still working on getting the data displayed in Fortran. I've been at it for a couple hours and cannot figure out the problem. I am getting an end of the file runtime error. Here is the code:
Code:
   PROGRAM testx
      IMPLICIT NONE
      CHARACTER(20) :: File Name
      INTEGER :: Count = 0, OpenStatus, InputStatus
      REAL :: Y, M, D, Rain, Tmax, Tmin, srain, stmax, stmin
      WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter name of data file:"
      READ *, FileName
      OPEN (UNIT = 15, FILE = FileName, STATUS = "OLD")
      READ(15,*) Y, M, D, Rain, Tmax, Tmin
 100  FORMAT (6x)
      PRINT 100, "Year", "Month", "Day", "Rainfall", "MxTemp", "MiTemp"
      END PROGRAM testx

Here is the text file containing the data I am working with:
View attachment 42527
I suspect that your code is reading the data correctly, but your PRINT statement is just printing the strings, not the values of the variables. Also, your format statement is not what you want.

You are going to need a DO loop of some kind to iterate through the data in your input file. Your code above reads one set of six items and no more.

I did a search for "fortran format statement". This is the first of many links I got: http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html.
 
Okay I changed the code around a bit. It compiles but the program itself is giving me an error: "STOP ***Input error***" I'm not sure how to fix this.

Code:
      PROGRAM testx
      IMPLICIT NONE
      CHARACTER(20) :: FileName
      INTEGER :: Count = 0, OpenStatus, InputStatus
      REAL :: Y, M, D, Rain, Tmax, Tmin, srain, stmax, stmin
      ! Open the file as unit 15. 
      ! Input and output formats and proper display
      WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter name of data file:"
      READ *, FileName
      OPEN (15, FILE = FileName, STATUS = "OLD", IOSTAT=OpenStatus)
      IF (OpenStatus > 0) STOP "***Cannot open the file ***"
 99   FORMAT (1X, A5, 1X, A5, 1X, A4, 1X, A8, A8, A8)
 100  FORMAT (1X, I4, 1X, I2, 1X, I2, 1X, F4.2, 1X, F4.2, F4.2)
      PRINT 99, "Year", "Month", "Day", "Rainfall", "MaxTemp", "MinTemp"
      PRINT 99, "====", "=====", "===", "========", "=======", "======="
      ! Read the data line by line
      ! Display the data in a table
      DO
       READ (15, FMT=100, IOSTAT=InputStatus) Y, M, D, Rain, Tmax, Tmin
       IF (InputStatus > 0) STOP "***Input error ***"
       IF (InputStatus < 0) EXIT ! end of file
       PRINT 100, Y, M, D, Rain, Tmax, Tmin
      END DO  
     
      END PROGRAM testx

To try and identify the error I took out the
Code:
IF (InputStatus > 0) STOP "***Input error ***"
and got a error from Fortran saying that it expected an Integer but got a real, yet the first number in the text file is an integer.
 
Last edited:
Are you still using the same test file as attached in your first post? That file doesn't match the FORMAT statement for your READ statement.
 
jtbell said:
Are you still using the same test file as attached in your first post? That file doesn't match the FORMAT statement for your READ statement.

Yes, where there were commas there are now spaces. So the file at the beginning looks like:

1948 8 1 0.02 89.0 71.0
1948 8 2 0.00 90.0 72.0
And so on...

The 100 FORMAT statement reads space first so I took the "1X" out of the code in my last post. Then the next is "I4" since the year is a 4 digit integer, then 1X for a space, and so for the next integer for day and month, then rational numbers for the rain, tmax, amd tmin. But I still get an error...

To further test this I removed the specified format and changed the read statement to
Code:
READ (15, FMT=*, IOSTAT=InputStatus) Y, M, D, Rain, Tmax, Tmin
.
I don't get an error but the data is not displaying on the screen. The only thing being printed is the table format, but not the actual values from the text file.
 
Last edited:
As I recall, if the data is separated by spaces, you don't need to specify a format, but can use the default format instead, so try that:

READ (15, FMT=*, IOSTAT=InputStatus) Y, M, D, Rain, Tmax, Tmin

or simply

READ (15, *, IOSTAT=InputStatus) Y, M, D, Rain, Tmax, Tmin

Just make sure the input file doesn't have a REAL value where the program expects to read an INTEGER.

You need to specify an input format mainly when the data is not separated by spaces. For example, if you have a date in the format 20120108 and want to read the year, month and day separately, you need to use the format I4, 2I2. This sort of thing was common in the dark ages of programming, when punched cards were the most common input medium and people didn't want to waste any of the 80 columns by putting unnecessary blank spaces in them.
 
jtbell said:
As I recall, if the data is separated by spaces, you don't need to specify a format, but can use the default format instead, so try that:

READ (15, FMT=*, IOSTAT=InputStatus) Y, M, D, Rain, Tmax, Tmin

or simply

READ (15, *, IOSTAT=InputStatus) Y, M, D, Rain, Tmax, Tmin

Just make sure the input file doesn't have a REAL value where the program expects to read an INTEGER.

You need to specify an input format mainly when the data is not separated by spaces. For example, if you have a date in the format 20120108 and want to read the year, month and day separately, you need to use the format I4, 2I2. This sort of thing was common in the dark ages of programming, when punched cards were the most common input medium and people didn't want to waste any of the 80 columns by putting unnecessary blank spaces in them.
Yes, I tried that see the post above you. No error in the program but the numerical data from the text file is not displaying for some reason.

Here's what my code looks like now:

Code:
      PROGRAM testx
      IMPLICIT NONE
      CHARACTER(20) :: FileName
      INTEGER :: Count = 0, OpenStatus, InputStatus, Y, M, D
      REAL ::  Rain, Tmax, Tmin, srain, stmax, stmin
      ! Open the file as unit 15. 
      ! Input and output formats and proper display
      WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter name of data file:"
      READ *, FileName
      OPEN (15, FILE = FileName, STATUS = "OLD", IOSTAT=OpenStatus)
      IF (OpenStatus > 0) STOP "***Cannot open the file ***"
 99   FORMAT (1X, A5, 1X, A5, 1X, A4, 1X, A8, A8, A8)
 100  FORMAT (I4, 1X, I2, 1X, I2, 1X, F4.2, 1X, F4.2, F4.2)
      PRINT 99, "Year", "Month", "Day", "Rainfall", "MaxTemp", "MinTemp"
      PRINT 99, "====", "=====", "===", "========", "=======", "======="
      ! Read the data line by line
      ! Display the data in a table
      DO
       READ (15, FMT=*, IOSTAT=InputStatus) Y, M, D, Rain, Tmax, Tmin
       IF (InputStatus > 0) STOP "*** Input error ***"
       IF (InputStatus < 0) EXIT ! end of file
       PRINT *, Y, M, D, Rain, Tmax, Tmin      
      END DO  
     
      END PROGRAM testx
 
Last edited:
I don't see anything obvious. After you open the file, you stop if OpenStatus > 0. You aren't checking for negative values, so if one occurs, that will affect reading data from the file.
 
Mark44 said:
I don't see anything obvious. After you open the file, you stop if OpenStatus > 0. You aren't checking for negative values, so if one occurs, that will affect reading data from the file.

I fixed the FORMAT statement in my code and got the data read and displayed correctly.
 
  • #10
Azorio said:
I am trying to read and display the content of a text file in Fotran 90 to make sure that fortran is interpreting the data correctly. Eventually I want to store the data in an array so I can calculate averages, but at the moment I am still working on getting the data displayed in Fortran. I've been at it for a couple hours and cannot figure out the problem. I am getting an end of the file runtime error. Here is the code:

PROGRAM testx
IMPLICIT NONE
CHARACTER(20) :: File Name
INTEGER :: Count = 0, OpenStatus, InputStatus
REAL :: Y, M, D, Rain, Tmax, Tmin, srain, stmax, stmin
WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter name of data file:"
READ *, FileName
OPEN (UNIT = 15, FILE = FileName, STATUS = "OLD")
READ(15,*) Y, M, D, Rain, Tmax, Tmin
100 FORMAT (6x)
PRINT 100, "Year", "Month", "Day", "Rainfall", "MxTemp", "MiTemp"
END PROGRAM testx

There are two errors in this code:
1. There's a space in the variable "File Name" in line 3.
2. There is no data format item in the FORMAT statement.
It needs to be something like
100 FORMAT (6X, 6A)
which provides the means for the six strings (the headings) to be
printed out.
 
  • #11
Azorio said:
I fixed the FORMAT statement in my code and got the data read and displayed correctly.

Can you explain the correction that you have done in your format statement.
what was the correction?
 
  • #12
Azorio said:
I fixed the FORMAT statement in my code and got the data read and displayed correctly.

What was the correction that you have done in the format statement?
 
  • #13
Rems, the OP is a year and a half old. I wouldn't wait too eagerly for a response.
 

Similar threads

Replies
12
Views
3K
Replies
5
Views
5K
Replies
5
Views
2K
Replies
2
Views
2K
Replies
4
Views
8K
Replies
2
Views
1K
Replies
4
Views
1K
Replies
33
Views
5K
Replies
19
Views
6K
Back
Top