Fortran 77 reading from text file, printing

In summary: By removing the 1X, the program will read the file starting in column 1, which is where the data is located. This will prevent the shifting and incorrect printing of data.
  • #1
dorado29
17
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
  • #2
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
 
  • #3
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.
 

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

In Fortran 77, you can use the READ statement to read data from a text file. The syntax is:

READ(unit number, format) variable1, variable2, ...

Where the unit number is the number associated with the text file, and the format specifies the data type of the variables being read.

2. Can I read in multiple lines of data from a text file using Fortran 77?

Yes, you can read in multiple lines of data from a text file in Fortran 77 by using a DO loop and the READ statement within the loop. This will allow you to read in each line of data until you reach the end of the file.

3. How can I print data from a text file in Fortran 77?

To print data from a text file in Fortran 77, you can use the WRITE statement. The syntax is:

WRITE(unit number, format) variable1, variable2, ...

Where the unit number is the number associated with the text file, and the format specifies the data type and format of the variables being written.

4. Can I specify the format of the data when printing from a text file in Fortran 77?

Yes, you can specify the format of the data when printing from a text file in Fortran 77. This can be done by using the format specifier within the WRITE statement. For example, if you want to print a floating point number with 2 decimal places, you can use the format specifier F2.

5. How do I close a text file in Fortran 77?

To close a text file in Fortran 77, you can use the CLOSE statement. The syntax is:

CLOSE(unit number)

This will close the text file associated with the specified unit number. It is important to close files after using them to avoid any potential errors or data loss.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
11K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
19K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
Back
Top