- #1
kouhei
- 1
- 0
Homework Statement
Hello.
A student came to me by yesterday with a question like this:
Here is a sample file (sample.dat),
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
and also with many result files, such as result_1.dat:
1 0.1
2 0.2
3 0.3
or result_2.dat
1 0
5 10
9 0.3
10 20
The student would like to write a fortran to sum numbers at the second column. (so if we take only two result files, the final 'result' will be:)
1 0.1
2 0.2
3 0.3
4 0
5 10
6 0
7 0
8 0
9 0.3
10 20
I tried to solve this question but it doesn't work well, so far my program looks like this:
program Main
implicit none
integer GetFileN,ios,nmax1,nmax2
character(160) filename,tmp
integer i,j,m,n
integer x(1:20),a(1:20)
real y,b
integer summ
filename='result_'
print*, 'Please enter the number of files:'
read*,n
do m=1,n
write(tmp,*)m
open(1,file=trim(filename)//trim(adjustl(tmp))
1//'.dat',status='old' )
nmax1=GetFileN(1)
do i = 1,nmax1
read(1,*,iostat=ios) x(i),y
if(ios /=0) then
exit
endif
c write(6,*) x(i),y
open(10,file='sample.dat',status='old')
nmax2=GetFileN(10)
do j=1,nmax2
read(10,*,iostat=ios)a(j),b
if(ios/=0) then
exit
endif
c write(6,*)a(j),b
summ=0
if (any(x==a)) then
summ=b+y
else
summ=b
endif
open(11,status='unknown',file='meanlifetime.dat')
write(11,*)a(j),summ
enddo
enddo
enddo
close(1)
close(10)
close(11)
end
integer function GetFileN(iFileUnit)
implicit none
logical , parameter :: b = .True.
integer , intent( IN ) :: iFileUnit
character*(1) :: c
GetFileN = 0
rewind( iFileUnit )
do while (b)
read( iFileUnit , * ,end =999 ,Err = 999 )c
GetFileN = GetFileN + 1
end Do
999 rewind( iFileUnit )
return
end function GetFileN
Does anyone have an idea?