新人求助,想去除一个文件中负数
应该很简单的问题。搞了一整天才搞定怎么读取、写入。文件如果水深为负数则整行去除,不知到怎么改。怎么改if a(:,i)能表示在TXT中得位置呢
文件什么样子? 我发来。处理一半的程序和文件 Program main
Use DFile_Mod
Implicit None
Real , allocatable :: a(:,:)
Character(len=512) :: cStr
Real :: r
integer :: FILE_IN , i , nRow , nCol
Open( NewUnit = FILE_IN , File = "text.asc" )
nRow = GetFileN( FILE_IN ) - 2 !//获得文件行数
Read( FILE_IN , * ) !//跳过第一行
Read( FILE_IN , '(a512)' ) cStr!//读取第三行
nCol = GetDataN( cStr ) - 8 !//获得第三行有多少列
write( *, '("Row:",g0," Col: ",g0)' ) nRow , nCol
Allocate( a( nCol , nRow ) )
Backspace( FILE_IN ) !//退回到第三行
open( 11 , file ="tx1.txt")
write( 11 , * )"depth temp sal turb"
Do i = 1 , nRow
Read( FILE_IN , * ) r ,r, r, r, a( : , i )
write( 11 , * ) a(:,i)
End Do
Deallocate( a )
Close( FILE_IN )
End Program main 上面有模版,不过没粘贴 想把所有水深是负的行都去掉,因为是误差值 text.asc 是什么样子? 原始文档就是这个。网站不让传ASC,我复制到TXT了。没啥区别
只是筛选数据的话,就简单了。
Program www_fcode_cn
implicit none
character(len=512) :: c
character(len=1) :: s
integer :: FILE_IN , FILE_OUT , k , i
real :: DepSM
Open( NewUnit = FILE_IN , File = "text.asc" )
Open( NewUnit = FILE_OUT, File = "text.txt" )
Read( FILE_IN , 100 ) c
Write( FILE_OUT , 99 ) trim(c)
Do
Read( FILE_IN , 100 , ioStat = k ) c
if ( k /= 0 ) exit
Read( c , * ) i , i , i , s , DepSM
If ( DepSM > 0.0 ) then
Write( FILE_OUT , 99 ) trim(c)
End If
End Do
Close( FILE_IN )
Close( FILE_OUT )
100 format(a512)
99format(a)
End Program www_fcode_cn 厉害了,我得哥。有效果诶。。 不过程序没看明白,我好好研究一下。昨天九点多走的时候没发现有回复啊,奇怪。Ths!:-victory:
页:
[1]