CSC 272 - Software II: Principles of Programming Languages

Dr. R. M. Siegfried

FORTRAN Program to Read An Array of Ten Integers

c   There is an array of ten integers
c   One 30-character string for the file name
c
      integer array(10)
      character*30 filename
c
c     Formatless output and formatted in input reading in 30 characters
      write(*,*) 'Enter filename'
      read(*,501) filename
  501 format(a30)
c
c     Open it as device # 1 in I/O statements and it exists
c

      open(unit=1, file=filename, status='old')
c
c     FOR i:= 1 TO 10 DO
c

      do 101 i = 1, 10
c
c       Read from unit 1 using Format statement 502.
c	At end of file go to statement 102 and on error go to 103
c       The one input field is a 5-digit integer 
c       Blanks are taken as zeros
c
        read(1,502, END=102, ERR=103) array(i)
  502   format(i5)
        write(*, *) array(i)
  101 continue
  102 continue
  103 continue
      write(*,*) filename
      end

[Back to the FORTRAN Index]