Reading and Displaying text file in Fortran

In summary: END DO ! Display the data in a table IF (Count > 0) PRINT *, "(f10.5)", FMT=10.5, IOSTAT=InputStatus ELSE PRINT *, "(f10.4)", FMT=10.4, IOSTAT=InputStatus END IF ! Close the file CLOSE (15) ! Print the data WRITE (*, FMT=100, IOSTAT=InputStatus)
  • #1
Azorio
6
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
  • #2
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.
 
  • #3
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:
  • #4
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.
 
  • #5
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:
  • #6
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.
 
  • #7
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:
  • #8
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.
 
  • #9
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.
 

Related to Reading and Displaying text file in Fortran

1. How do I read a text file in Fortran?

To read a text file in Fortran, you can use the OPEN statement to open the file, followed by the READ statement to read the contents of the file. You will need to specify the file name and the format of the data in the file. You can also use the INQUIRE statement to check the status of the file after it has been opened.

2. How do I display the contents of a text file in Fortran?

To display the contents of a text file in Fortran, you can use the WRITE statement to print the data to the screen. You will need to specify the file name and the variables you want to display. You can also use the FORMAT statement to specify the format of the output.

3. Can I read and display a text file in Fortran simultaneously?

Yes, you can use the OPEN, READ, and WRITE statements in combination to read and display a text file in Fortran simultaneously. You will need to specify the file name and format for both the input and output operations.

4. How do I handle errors while reading and displaying a text file in Fortran?

To handle errors while reading and displaying a text file in Fortran, you can use the ERROR keyword in the OPEN statement to specify a label that will be executed if an error occurs during the file operation. You can also use the IOSTAT variable to check the status of the operation and handle any errors accordingly.

5. Can I read and display a specific line from a text file in Fortran?

Yes, you can use the READ statement with the UNIT and REC keywords to read a specific line from a text file in Fortran. You will need to specify the file name and the line number you want to read. You can then use the WRITE statement to display the contents of that line.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
4
Views
879
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
8K
  • Programming and Computer Science
Replies
33
Views
4K
  • Programming and Computer Science
Replies
19
Views
6K
  • Programming and Computer Science
Replies
10
Views
4K
Back
Top