Comp Sci Fortran 77 reading from text file, printing

AI Thread Summary
The discussion revolves around a Fortran 77 program designed to read time, temperature, pressure, and volume data from a text file, converting military time to standard time format. The user encountered issues with incorrect data being read and printed, specifically with temperature values appearing shifted. It was determined that removing the "1X" from the format statements resolved the issue, as it prevented the program from misaligning the data read from the file. The final program successfully calculates and displays the averages for temperature, pressure, and volume after processing the data. The resolution highlights the importance of format specifications in data reading operations in Fortran.
dorado29
Messages
17
Reaction score
0

Homework Statement



Write a program that reads the time, temperature, pressure, and volume measurements from a data file like :

0900015001990700
1000019602210877
1100021202790925
1234034203221015
1300038803221121
1400044803241425
1503051303201520
1604055503181665
1700061303191865
1800067503232080
1900072103282262
2005076803252564
2100083503272869
2200088903303186

The program has to convert the first 4 digits of each line from military time to a result that prints out in standard time with AM and PM indicated. The program has to print out that time and the temp, pressure and volume in this form:

Time Temp Press Volume
======================================
12:00PM 34.2 32.2 101.5
- - - -
- - - -
10:00PM 88.9 33.0 318.6
======================================
Averages 75.4 41.7 193.2



Each 4 numbers from the data line apply to time, temp, pressure and volume respectively

0900015001990700

"0900" is the time, "0150" is temperature, etc




Here's what I've done so far:
PROGRAM TABLE

CHARACTER*2 TIMVAL
INTEGER COUNT, EOF, MLTIME, A, B
REAL TEMP, VOL, PRES, AVTEMP, AVVOL, AVPRES
REAL SUMT, SUMV, SUMP
COUNT=0
SUMT=0.0
SUMP=0.0
SUMV=0.0
A = 0
B = 0
MLTIME=0

OPEN (UNIT = 15, FILE = 'FORMAT1.TXT', STATUS = 'OLD')

PRINT *

90 FORMAT(1X, A6, 6X, A11, 7X, A8, 6X, A6)

PRINT 90, 'TIME', 'TEMPERATURE', 'PRESSURE', 'VOLUME'
PRINT*,'======================================================='

100 FORMAT(1X, I2.2, ':', I2.2, A3, 7X, F4.1, 13X, F4.1, 8X, F5.1)
110 FORMAT(1X, I2.2, I2.2, T5, F4.1, T9, F4.1, T13, F4.1)


READ (UNIT = 15, FMT = 110, IOSTAT = EOF) A, B, TEMP, PRES, VOL

DO WHILE(EOF .GE. 0)

IF(A .GE. 12) THEN
TIMVAL = 'PM'

ELSE
TIMVAL = 'AM'
END IF

IF(A .GE. 13) THEN
A = A - 12
END IF

PRINT 100, A, B, TIMVAL, TEMP, PRES, VOL

COUNT = COUNT + 1
SUMT = SUMT + TEMP
SUMP = SUMP + PRES
SUMV = SUMV + VOL

READ (UNIT = 15, FMT = 110, IOSTAT = EOF) A, B, TEMP, PRES, VOL
END DO

(the averages part of the program should go here but I'm not having problems with
it)
END


I'll post a picture of the output shortly.. For some reason copy paste isn't being very nice.

Anywho, my problem lies with blanks and zeros. The first few lines of the ouput involving data is:

00:00AM 0.0 0.0 0.0
10:00AM 1.9 **** 108.7
11:00AM 2.1 **** 909.2
12:34PM **** **** 210.1
etc...

from the data
0900015001990700
1000019602210877
1100021202790925
1234034203221015

it seems that where the data is read from after "time" is shifted. instead of reading "0877" and printing "87.8" it reads "1087" and prints "108.7"

Do you guys see anything glaringly wrong with the program? My professor doesn't cover any of this and isn't available to help.
 
Physics news on Phys.org
i got it figured out, sorry :blushing:

my friend told me to take out the "1x" from the format 100 and 110 statements, along with some other tweaks. does anyone know why taking "1x" out makes it work?

heres my final program:

Code:
c     dorado29--COS215--3/30/11--Problem #7--Temp, Press, Vol, Time
      PROGRAM TABLE

      INTEGER COUNT, EOF, A, B
      REAL TEMP, VOLUME, PRESS, AVTEMP, AVVOL, AVPRES, SUMT, SUMV, SUMP
      CHARACTER*2 AMPM

      COUNT=0
      SUMT=0.0
      SUMP=0.0
      SUMV=0.0
      A = 0
      B = 0

      OPEN (UNIT = 0, FILE = 'FINAL.TXT', STATUS = 'OLD')

      PRINT 1, 'TIME', 'TEMPERATURE', 'PRESSURE', 'VOLUME'
    1 FORMAT(1X, A6, 6X, A11, 7X, A8, 6X, A6)
      PRINT*,'======================================================='

    2 FORMAT(I2, ':', I2.2, A3, 7X, F4.1, 13X, F4.1, 8X, F5.1)
    3 FORMAT(I2, I2.2, T5, F4.1, T9, F4.1, T13, F4.1)

      READ (UNIT = 0, FMT = 3, IOSTAT = EOF) A, B, TEMP, PRESS, VOLUME

c     ******************************************************************
      DO WHILE(EOF .GE. 0)

                       IF(A .GE. 12) THEN
                       AMPM = 'PM'

                              ELSE
                              AMPM = 'AM'
                       END IF

                       IF(A .GE. 13) THEN
                               A = A - 12
                       END IF

                 PRINT 2, A, B, AMPM, TEMP, PRESS, VOLUME

                 COUNT = COUNT + 1

                 SUMP = SUMP + PRESS
                 SUMV = SUMV + VOLUME
                 SUMT = SUMT + TEMP

      READ (UNIT = 0, FMT = 3, IOSTAT = EOF) A, B, TEMP, PRESS, VOLUME

      END DO
c     ******************************************************************

      PRINT*,'======================================================='

    4 FORMAT(1X, 'AVERAGES', 6X, F4.1, 13X, F4.1, 8X, F5.1)

      AVTEMP = SUMT / COUNT
      AVPRES = SUMP / COUNT
      AVVOL = SUMV / COUNT

      PRINT 4, AVTEMP, AVPRES, AVVOL

      CLOSE(0)

      PAUSE
      END
 
Unless the data in your text file starts in column 2, then when the program reads the file, it will start 1 column to the right due to the 1X descriptor in the FORMAT statement.
 

Similar threads

Back
Top