[Fortran] 纯文本查看 复制代码 Module typedef
Implicit None
private
Integer , parameter :: N = 13
type , public :: ST_Mesh
character(len=12) :: geom , out
real :: origin(3)
real :: iMesh(N) , jMesh(N) , kMesh(N)
Integer :: iInts(N) , jInts(N) , kInts(N)
contains
Procedure :: ReadFrom
end type ST_Mesh
contains
Logical Function ReadFrom( this , fu ) result( bOK )
Class(ST_Mesh) :: this
Integer , Intent(IN) :: fu
character(len=2560) :: buf !//足够大的缓冲
character(len=512) :: cRead
integer :: i , j , m , k , n
bOK = .false.
n = 0
Do
Read( fu , "(a512)" , ioStat = k ) cRead
if( k/=0 ) exit
!if( ) exit //其他停止依据,可添加在这
k = len_trim(cRead)
if( k <= 0 ) cycle
buf(n+1:) = trim(cRead)
n = n + k + 1
buf(n:n) = " "
End Do
if( n <= 0 ) return
j = 1
Do
k = Index( buf(j:) , '=' )
if( k < 2 ) exit
m = k + j - 1
i = Index( buf(:m) , ' ' , .true. )
if( i < 0 ) exit
Select Case( buf(i+1:m-1) )
Case( 'GEOM' )
Read( buf(m+1:) , * , ioStat = k ) this%geom
Case( 'ORIGIN' )
Read( buf(m+1:) , * , ioStat = k ) this%origin
Case( 'imesh' )
Read( buf(m+1:) , * , ioStat = k ) this%imesh
Case( 'iints' )
Read( buf(m+1:) , * , ioStat = k ) this%iints
Case( 'jmesh' )
Read( buf(m+1:) , * , ioStat = k ) this%jmesh
Case( 'jints' )
Read( buf(m+1:) , * , ioStat = k ) this%jints
Case( 'kmesh' )
Read( buf(m+1:) , * , ioStat = k ) this%kmesh
Case( 'kints' )
Read( buf(m+1:) , * , ioStat = k ) this%kints
Case( 'out' )
Read( buf(m+1:) , * , ioStat = k ) this%out
End Select
if( k/=0 ) return
j = m + 1
End Do
bOK = .true.
End Function ReadFrom
End Module typedef
Program Main
use typedef
Implicit None
type( ST_Mesh ) :: mesh
Logical :: ok
Open(12,File="File.txt")
ok = mesh%ReadFrom(12)
if( ok ) write(*,*) mesh
Close(12)
End Program Main |