Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read and Display data in FORTRAN 1

Status
Not open for further replies.

Azorio

Programmer
Jan 7, 2012
10
US
I am trying to read and display data from a text file in FORTRAN. Eventually I want to store the data in an array to calculate specific averages but right now I am having trouble getting the data to be read and displayed. With the code below I am getting a program error "STOP ***Input error***". Help would be much appreciated. Below is the code I have been working on and here is a link to the text file containing the data
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
 
Replace "READ(15,FMT=100,..." by "READ(15,FMT=*,..." and try again.

If you have written the file 15 by hand, the risk of not strictly following the fixed format is huge. The format "*" will manage automatically the deviations (for instance an extra space somewhere).

About IOSTAT, I don't remember that a negative one means an end of file. I don't believe that this is required by the norm. But this can work for a specific compiler anyway...

François Jacq
 
Also, you may want to declare year, month and day as INTEGERs for a even more correct list-directed input...I don't remember exactly, but when I switched from f77 to f90, I started to have a little problem with my input files in regards to reading integers into real variables...I think.

Also, I don't particularly like your "DO - ENDDO" from where you never get out of...soon enough, you will need to do something else in this program; you need to fix that...maybe with a "DO WHILE - ENDDO" at least.



 
Fjacq, I rewrote the READ statement as you said and I do not get a program error; however, the numerical data is not being printed to the screen.

Salgerman, I changed the Y, M, D to integers. I'm not sure what you mean with using a DO WHILE in my program.
 
Azorio:

When I downloaded your file for testing, I was not able to read the file...I blamed this to possible corruption during upload or download...but if even you cannot read the file, maybe there is something wrong with the file...you may have to at least copy from one editor to another (different program!) or something. What I was experiencing was that, somehow, I was only being able to read the last line, while all other one were being skipped.

After messing around with your source for the wrong reasons, my latest version of the program ended up being the following one:

Code:
      PROGRAM testx
      IMPLICIT NONE
      INTEGER Y, M, D
      REAL    Rain, Tmax, Tmin, srain, stmax, stmin
 99   FORMAT(1X, A5, 1X, A5, 1X, A4, 1X, A8, A8, A8)
 100  FORMAT(1X, I5, 3X, I2, 4X, I2, 2X, F5.2, 4X, F5.2, 3X, F5.2)
      write(*,*) " Year Month  Day Rainfall MaxTemp MinTemp"
      write(*,*) " ==== =====  === ======== ======= ======="
      do while (.true.)
        read(*,*,end=120) Y, M, D, Rain, Tmax, Tmin
        write(*,100) Y, M, D, Rain, Tmax, Tmin
      end do
 120  continue
      END PROGRAM testx

By the way, the manner in which I run this program without having to open any files is via input re-direction, like so:

testx < inputfile.txt

Hope this helps.

 
Salgerman, I am not trying to write the values to a new file. I just want the display to show up in the terminal via PRINT statement. I've tried using a different text editor and that did not work. I don't want to deviate much from my code unless it's absolutely necessary. ie opening the file using the OPEN statement.
 
I ran your program including using testx < testx.txt and it still will not work. Maybe there is something wrong with the file? I don't know what to do.
 
I understand that you are not trying to write the values to a new file, at least not using your program...what I was suggesting is what I had to do to get my program to run...to create a brand new file with your data...just in case there are some invisible, non-complaint characters in your file that are making the READ statement now quite work...

Debugging a program consists of some detective work; so, put your hat on and start with baby steps.

For example, open your editor and type by hand the first 2 or 3 lines of your existing file; save the text as a brand new file...see if you can now read these two lines from the new file. Keep testing your code and mine...



 
Salgerman, I tried typing out the values myself by hand and you were right. You're code works beautifully, my code reads the fine but when it comes to the real numbers it's carrying them out to 6 decimal places.There must be some characters that are not allowing my read statement to work correctly. But its strange because looking through the original document I cannot spot the error. Is there a way to search for this?
 
Okay it all works now including the original file. I think changing the FORMAT statement made the difference. Thank you very much.
 
Hi Azorio,
salgerman helped you to solve your problem, but you forgot to give him a star for his effort.
 
mikrom,
Sorry, I wasn't aware of the etiquette.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top