0

I have a Fortran code which reads a txt file with seperate lines of characters and digits and then write them in a 1D array with 20 elements. This code is not compatible with Fortran 77 compiler Force 2.0.9. My question is that how we can apply the aformenetioned procedure using a Fortran 77 compiler;i.e defining a 1D array nd then write the txt file line by line into elements of the array?

Thank you in advance.

The txt file follows:

Case 1:

10  0  1  2  0

1.104  1.008  0.6   5.0

25  125.0  175.0  0.7  1000.0

0.60

1  5

Advanced Case

15 53 0 10 0 1 0 0 1 0 0 0 0

0 0 0 0

0 0 1500.0 0 0 .03

0 0.001 0

0.1 0 0.125 0.08 0.46

0.1 5.0 0.04

@ Jason: I am a beginner and still learning Fortran. I guess Force 2 uses g77. The followings are the correspond part of the original code. Force 2 editor returns an empty txt file as a result.

       DIMENSION CARD(20)
       CHARACTER*64 FILENAME
       DATA XHEND / 4HEND  /

       OPEN(UNIT=3,FILE='CON')
       OPEN(UNIT=4,FILE='CON')
       OPEN(UNIT=7,STATUS='SCRATCH')

       WRITE(3,9000) 'PLEASE ENTER THE INPUT FILE NAME : '
  9000 FORMAT (A)
       READ(4,9000) FILENAME

       OPEN(UNIT=5,FILE=FILENAME,STATUS='OLD')
       WRITE(3,9000) 'PLEASE ENTER THE OUTPUT FILE NAME : '
       READ(4,9000) FILENAME
       OPEN(UNIT=6,FILE=FILENAME,STATUS='NEW')
       FILENAME = '...'

       IR = 7
       IW = 6
       IP = 15
  5    REWIND IR
       I = 0
  2    READ (5,7204,END=10000) CARD
       IF (I .EQ. 0 ) WRITE (IW,7000)
  7000 FORMAT (1H1 / 10X,15HINPUT DECK ECHO / 10X,15(1H-))
       I= I + 1
       WRITE (IW,9204)  I,CARD
       IF (CARD(1) .EQ. XHEND ) GO TO 7020
       WRITE (IR,7204) CARD
  7204 FORMAT (20A4)
  9204 FORMAT (1X,I4,2X,20A4)
       GO TO 2
  7020 REWIND IR         
milancurcic
  • 6,202
  • 2
  • 34
  • 47
John Tab
  • 35
  • 4
  • 2
    Please post code. Also note that Force is not a compiler, but an editor. It is using either gfortran, g95 or g77. Do you know which one? Also, please post code. – milancurcic Jul 27 '12 at 21:34

1 Answers1

4

It looks that CARD is being used as a to hold 20 4-character strings. I don't see the declaration as a character variable, only as an array, so perhaps in extremely old FORTRAN style a non-character variable is being used to hold characters? You are using a 20A4 format, so the values have to be positioned in the file precisely as 20 groups of 4 characters. You have to add blanks so that they are aligned into groups of 4 columns.

If you want to read numbers it would be much easier to read them into a numeric type and use list-directed IO:

real values (20)
read (5, *) values

Then you wouldn't have to worry about precision positioning of the values in the file.

This is really archaic FORTRAN ... even pre-FORTRAN-77 in style. I can't remember the last time that I saw Hollerith (H) formats! Where are you learning this from?

Edit: While I like Fortran for many programming tasks, I wouldn't use FORTRAN 66! Computers are supposed to make things easier ... there is no reason to have to count characters. Instead of

7000 FORMAT (1H1 / 10X,15HINPUT DECK ECHO / 10X,15(1H-))

You can use

7000 FORMAT ( / 10X, "INPUT DECK ECHO" / 10X, 15("-") )

I can think of only two reasons to use a Hollerith code: not bothering to change legacy source code (it is remarkable that a current Fortran compiler can process a feature that was obsolete 30 years ago! Fortran source code never dies!), or studying the history of computing languages. The name honors a great computing pioneer, whose invention accomplished the 1890 US Census in one year, when the 1880 Census took eight years: http://en.wikipedia.org/wiki/Herman_Hollerith

I much doubt that you will see the "1" in the first column performing "carriage control" today. I had to look up that "1" was the code for page eject. You are much more likely to see it in your output. See Are Fortran control characters (carriage control) still implemented in compilers?

Community
  • 1
  • 1
M. S. B.
  • 28,968
  • 2
  • 46
  • 73
  • I still routinely write f77 code (complete with `blockdata` and `equivalence` -- fortunately no `entry` though) due to external constraints (don't ask) and I've never seen a Hollerith format – mgilson Jul 28 '12 at 01:04