QQ图片20181101153214.png (22.58 KB, 下载次数: 373)
fcode 发表于 2018-11-1 18:17
像这种“异形”文本文件的读取,可以很简单,也可以很复杂。
取决于一些事情,比如:
1. 数据文件中关键字 ...
fcode 发表于 2018-11-2 08:38
你给一个复制粘贴的数据文件(或部分),不要截图。
shurrik 发表于 2018-11-2 08:53
fmesh4:n GEOM=xyz ORIGIN=-338.760 -338.760 -213.526
imesh= -161.342 -139.8575 -118.34 ...
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
fcode 发表于 2018-11-2 09:48
[mw_shl_code=fortran,true]Module typedef
Implicit None
private
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) | Powered by Discuz! X3.2 |