Silverfrost Forums

Welcome to our forums

How to code for processing multiple external data files?

3 Feb 2010 2:51 #5869

Hi,

I have a small program to comupte the sum from an external data file. The code is as follows,

!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

3 Feb 2010 3:43 #5871

Is this question different from the one you raised in https://forums.silverfrost.com/Forum/Topic/1253

3 Feb 2010 4:57 #5872

Thanks a lot. My bad I completely forgot about this post. Cheers!

Please login to reply.