pban92
Joined: 23 Nov 2009 Posts: 38
|
Posted: Wed Feb 03, 2010 3:51 pm Post subject: How to code for processing multiple external data files? |
|
|
Hi,
I have a small program to comupte the sum from an external data file. The code is as follows,
Code: |
!Program to read in the data FROM a file, sum and write out TO a file.
PROGRAM solution9
IMPLICIT NONE
INTEGER :: n
REAL :: sum
REAL, DIMENSION(40) :: filename
n = 40
CALL READFILE(filename,n)
CALL SUMDATA(filename,sum,n)
CALL WRITEDATA(sum)
CLOSE(UNIT = 3)
CLOSE(UNIT = 7)
STOP
END PROGRAM
!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
SUBROUTINE READFILE(x,rep)
IMPLICIT NONE
INTEGER :: i, ierr
INTEGER, INTENT(IN) :: rep
REAL, INTENT(OUT), DIMENSION(rep) :: x
ierr = 0
OPEN (UNIT=3, FILE='file1.txt', STATUS='OLD', ACTION='READ', IOSTAT=ierr) !READ from
DO i = 1, rep
READ (3,*,IOSTAT=ierr) x(i)
END DO
RETURN
END SUBROUTINE
!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
SUBROUTINE SUMDATA(row,total,rep)
IMPLICIT NONE
INTEGER :: i
INTEGER, INTENT(IN) :: rep
REAL, INTENT(IN), DIMENSION(rep) :: row
REAL, INTENT(OUT) :: total
total = 0
DO i = 1, rep
total = total + row(i)
END DO
RETURN
END SUBROUTINE
!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
SUBROUTINE WRITEDATA(output)
IMPLICIT NONE
INTEGER :: ierr
REAL, INTENT(IN) :: output
ierr = 0
OPEN (UNIT=7, FILE='output_file1.txt', STATUS='REPLACE', ACTION='WRITE', IOSTAT=ierr) !WRITE TO
WRITE(7,*) 'The sum of the data is ', output
RETURN
END SUBROUTINE
|
For multiple files like file2.txt, file3.txt and so on, rather than running the program by changing the file name separately, how can I modify the program, so multiple files can be processed at one click? Any hint/online example/book reference would be a great help!
Many many thanks in advance!
pban92 |
|