Fortran Coder

查看: 18857|回复: 9

[求助] fortran6.6文件续写

[复制链接]

5

帖子

1

主题

0

精华

新人

F 币
8 元
贡献
2 点
发表于 2014-1-24 19:53:09 | 显示全部楼层 |阅读模式
问题是这样,这是一个续写文件的循环,将前面得到的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  

135

帖子

15

主题

0

精华

版主

F 币
1159 元
贡献
637 点

爱心勋章管理勋章

发表于 2014-1-24 20:11:19 | 显示全部楼层
我对你的问题的理解是,你要把若干个optionx.txt 累积写到同一个option.txt 文件中去,写了4个都是按照顺序写的,到第5个的时候出问题了
试试
open(30,file=trim(name1),status='old',position='append')

1958

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1339 元
贡献
565 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

发表于 2014-1-24 20:23:50 | 显示全部楼层
嗯,如果还是有问题,可以试着上传数据文件和所有源代码(如果不是特别多的话)。

5

帖子

1

主题

0

精华

新人

F 币
8 元
贡献
2 点
 楼主| 发表于 2014-1-24 21:44:02 | 显示全部楼层
珊瑚虫 发表于 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










	




	
	
	

	
	


5

帖子

1

主题

0

精华

新人

F 币
8 元
贡献
2 点
 楼主| 发表于 2014-1-24 21:53:57 | 显示全部楼层
我主要是根据若干个diamond 3资料把一个地区所有的站点找出来,存在option.txt这个文件里。

1958

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1339 元
贡献
565 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

发表于 2014-1-24 22:03:20 | 显示全部楼层
你没给数据文件,我没有跑这个程序,只能肉眼看。不一定准确。

以下是猜想:

[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)

135

帖子

15

主题

0

精华

版主

F 币
1159 元
贡献
637 点

爱心勋章管理勋章

发表于 2014-1-24 22:10:00 | 显示全部楼层




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

        
        

5

帖子

1

主题

0

精华

新人

F 币
8 元
贡献
2 点
 楼主| 发表于 2014-1-24 22:30:49 | 显示全部楼层
fcode 发表于 2014-1-24 22:03
你没给数据文件,我没有跑这个程序,只能肉眼看。不一定准确。

以下是猜想:

果然是这样,加了close(30)之后就通了。真是万分感谢!!
但是还是不是很懂为什么有些情况下30就关不了呢?

1958

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1339 元
贡献
565 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

发表于 2014-1-24 22:34:07 | 显示全部楼层
在你的代码79行,有 close(30)
但是前面有跳转到 350 行的情况,此时,就没有执行 close(30)

5

帖子

1

主题

0

精华

新人

F 币
8 元
贡献
2 点
 楼主| 发表于 2014-1-24 22:55:25 | 显示全部楼层
fcode 发表于 2014-1-24 22:34
在你的代码79行,有 close(30)
但是前面有跳转到 350 行的情况,此时,就没有执行 close(30) ...

哦。明白了。谢谢你!
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-4-19 09:29

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表