Fortran Coder

标题: fortran6.6文件续写 [打印本页]

作者: why    时间: 2014-1-24 19:53
标题: fortran6.6文件续写
问题是这样,这是一个续写文件的循环,将前面得到的option1.txt中的内容续写到option.txt中,前四个文件续写都很成功,到第五个的时候就不从末尾续写了,而是从中间某一行写的。然后我用一个单独的fortran程序来续写第五个的时候,又是从末尾续写的,并没有出现错误。本人实在程序无能,纠结好几天未果,求大神解惑这是怎么了?
这是子程序代码:
[Fortran] 纯文本查看 复制代码
        subroutine filetofile(name1,name2)
        use aaa
        implicit none
        character(len=12) name1,name2
        character(len=8) stid
    real lon,lat
        integer high,ierr
        open(30,file=trim(name1),status='old',access='append')
        open(15,file=trim(name2))
        do
                read(15,*,iostat=ierr) stid,lon,lat,high
                if(ierr/=0) exit
                write(30,*) stid,lon,lat,high
                sum=sum+1
        enddo
        close(15)
        close(30)
        print*,sum       
        end subroutine filetofile  

作者: 珊瑚虫    时间: 2014-1-24 20:11
我对你的问题的理解是,你要把若干个optionx.txt 累积写到同一个option.txt 文件中去,写了4个都是按照顺序写的,到第5个的时候出问题了
试试
open(30,file=trim(name1),status='old',position='append')


作者: fcode    时间: 2014-1-24 20:23
嗯,如果还是有问题,可以试着上传数据文件和所有源代码(如果不是特别多的话)。
作者: why    时间: 2014-1-24 21:44
珊瑚虫 发表于 2014-1-24 20:11
我对你的问题的理解是,你要把若干个optionx.txt 累积写到同一个option.txt 文件中去,写了4个都是按照顺序 ...

还是不行。。。我把整个程序放上来。但是数据太大了传不了。。
[Fortran] 纯文本查看 复制代码
        module aaa
    integer,parameter::nt=1489,nl=61
        character(len=12) filename(nt),filename1(nl)
        integer sum
        end module       


        program readdata
        use aaa
        implicit none
        character(len=8) stid
    real lon,rain,lat
        integer i,j,high
        sum=0


        open(10,file='dir.txt')
        do i=1,nt
        read(10,*) filename(i)
        enddo

       
        open(20,file=filename(1))
        open(30,file='option.txt',form='formatted')
        do j=1,11
        read(20,*)
        enddo
100 read(20,*,end=200) stid,lon,lat,high,rain
        if((lon>97).and.(lon<109).and.(lat>26).and.(lat<35))        then
        write(30,*)  stid,lon,lat,high
        sum=sum+1
        endif
        goto 100
200 continue
        close(20)
        close(30)
        print*,sum



       

        do i=2,5
                print*,'*',i
                call writeoption(filename(i),'option.txt')
                call filetofile('option.txt','option1.txt')
        enddo
       
end       



        subroutine writeoption(name1,name2)
        use aaa
        character(len=12) name1,name2
        character(len=8) stid,stid1       
    real lon,lon1,rain,lat,lat1
        integer j,high,high1,t1,t2
        open(60,file=name1,status='old')
        do j=1,11
                read(60,*)
        enddo
        open(15,file='option1.txt',form='formatted',status='replace')
300 read(60,*,end=350) stid,lon,lat,high,rain
        t1=com1(lon,lat)
        if(t1==0)then
                goto 300
        else
                open(30,file=name2)
        400 read(30,*,end=450) stid1,lon1,lat1,high1
                t2=com2(trim(stid),trim(stid1))
                if(t2==0)then
                        goto 300       
                else
                        goto 400
                endif
        450 continue
                write(15,*)  stid,lon,lat,high
                close(30)
        endif
        goto 300
350 continue
    close(60)
        close(15)
        end subroutine  writeoption






        subroutine filetofile(name1,name2)
        use aaa
        implicit none
        character(len=12) name1,name2
        character(len=8) stid
    real lon,lat
        integer high,ierr
        open(30,file=trim(name1),status='old',position='append')
        open(15,file=trim(name2))
        do
                read(15,*,iostat=ierr) stid,lon,lat,high
                if(ierr/=0) exit
                write(30,*) stid,lon,lat,high
                sum=sum+1
        enddo
        close(15)
        close(30)
        print*,sum       
        end subroutine filetofile  






        function com1(a,b)
        use aaa
        real a,b
        if(((a>97).and.(a<109)).and.((b>26).and.(b<35)))        then       
                com1=1
        else
                com1=0
        endif
        end function



    function com2(x,y)
        use aaa
        character(len=8) x,y
        if(x==y)then
                com2=0
        else
                com2=1
        endif
        end function










       




       
       
       

       
       



作者: why    时间: 2014-1-24 21:53
我主要是根据若干个diamond 3资料把一个地区所有的站点找出来,存在option.txt这个文件里。
作者: fcode    时间: 2014-1-24 22:03
你没给数据文件,我没有跑这个程序,只能肉眼看。不一定准确。

以下是猜想:

[Fortran] 纯文本查看 复制代码
do i=2,5
                print*,'*',i
                call writeoption(filename(i),'option.txt')
                call filetofile('option.txt','option1.txt')
        enddo


这里先调用 writeoption,然后调用 filetofile

在 writeoption 里,通过 open(30,file=name2) 打开了 option.txt ,而且不是 append 的。
在某些情况下,30 会被 Close,某些情况下,不会被 Close。

而到了 filetofile 函数内,直接就 Open(30),如果此时 30 在之前并没有关闭,这里的 open(30,file=trim(name1),status='old',position='append') 就会失败

你试试在 writeoption 函数里,把 30 换成其他的。
或者在 filetofile 一开始,先 close(30)

作者: 珊瑚虫    时间: 2014-1-24 22:10




恩, 如果还不行 在每次写option 之前 加上 endfile(通道号)
        

        
        

作者: why    时间: 2014-1-24 22:30
fcode 发表于 2014-1-24 22:03
你没给数据文件,我没有跑这个程序,只能肉眼看。不一定准确。

以下是猜想:

果然是这样,加了close(30)之后就通了。真是万分感谢!!
但是还是不是很懂为什么有些情况下30就关不了呢?
作者: fcode    时间: 2014-1-24 22:34
在你的代码79行,有 close(30)
但是前面有跳转到 350 行的情况,此时,就没有执行 close(30)
作者: why    时间: 2014-1-24 22:55
fcode 发表于 2014-1-24 22:34
在你的代码79行,有 close(30)
但是前面有跳转到 350 行的情况,此时,就没有执行 close(30) ...

哦。明白了。谢谢你!




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2