登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 叩容 于 2013-12-18 18:44 编辑
我是读取站点数据遇到这个问题的。
有几百个文件,每个文件因为站点数不固定(检查剔除站点的技巧,此处不涉及),导致了文件的行数不一致。
现在的问题是怎样读取不确定大小的数据呢?
(如果只有1个文件我当然可以打开看一眼有几行再设置程序。但是如果有几百个,手动操作很不现实。)
所以我在百度上搜到了这个小技巧,顺便分享给大家。
 - Program Main
- Implicit None
- integer n
- integer :: GetFileN
- Open( 12 , File = 'a.txt' )
- n = GetFileN( 12 )
- Write( * , * ) '共有',n,'行!'
- Close( 12 )
- End Program Main
- Integer Function GetFileN( iFileUnit )
- Implicit None
- Integer , Intent( IN ) :: iFileUnit
- Integer :: ioS
- Character(Len=1) :: cDummy
- GetFileN = 0
- Rewind( iFileUnit )
- Do
- Read( iFileUnit , * , ioStat = ioS ) cDummy
- if ( ioS /= 0 ) Exit
- GetFileN = GetFileN + 1
- End Do
- Rewind( iFileUnit )
- Return
- End Function GetFileN
这个Function GetFileN 即可以确定打开的这个文件有多少行,后面的按顺序读取就行了。
原网址: fortran如何读取文件最后一行数据??
==========================
更新:
topmad童鞋的方法,请点击:
http://bbs.06climate.com/forum.php?mod=viewthread&tid=976
|