Fortran Coder

标题: 求助一个文件读写的问题 [打印本页]

作者: liux578    时间: 2015-8-17 20:27
标题: 求助一个文件读写的问题
如图所示的数据文件,我想用fortran把X Y Z对应下面的数据存入到一个数组中和写入到一个文件中,要求只存X Y Z的数据(坐标),其他的不要,请问该怎么操作?多谢指导

gp_pos.txt

3.43 KB, 下载次数: 1

原数据文件


作者: 楚香饭    时间: 2015-8-17 20:35
[Fortran] 纯文本查看 复制代码
Program www_fcode_cn
  Implicit None
  Integer :: i , ierr
  real :: x , y , z
  character(len=32) :: c
  Open( 12 , File = "gp_pos.txt" )
  Open( 13 , File = "gp_pos_new.txt" )
  Do i = 1 , 3
    read( 12 , * ) c
  End Do
  Do
    read( 12 , * , iostat = ierr ) i , c , x , y , c
    if ( ierr /= 0 ) exit
    i = len_trim( c )
    read( c(1:i-1) , * ) z
    write( 13 , * ) x , y , z
  End Do
  Close( 12 )
  Close( 13 )
End Program www_fcode_cn

作者: liux578    时间: 2015-8-17 21:23
非常感谢
作者: liux578    时间: 2015-8-18 20:44
群主,请问怎样把读出来的数据赋值给一个数组啊
作者: liux578    时间: 2015-8-18 20:45
楚香饭 发表于 2015-8-17 20:35
[mw_shl_code=fortran,true]Program www_fcode_cn
  Implicit None
  Integer :: i , ierr

群主,请问怎样把读出来的数据赋值给一个数组啊
作者: 百事可乐    时间: 2015-8-18 21:24
[Fortran] 纯文本查看 复制代码
Program www_fcode_cn
  Implicit None
  Integer :: i , k , ierr , N , GetFileN
  real , allocatable :: x(:) , y(:) , z(:)
  character(len=32) :: c
  Open( 12 , File = "gp_pos.txt" )
  Open( 13 , File = "gp_pos_new.txt" )
  N = GetFileN( 12 ) - 3  
  allocate( x(N) , y(N) , z(N) )
  Do i = 1 , 3
    read( 12 , * ) c
  End Do
  Do i = 1 , N
    read( 12 , * , iostat = ierr ) c , c , x(i) , y(i) , c
    if ( ierr /= 0 ) exit
    k = len_trim( c )
    read( c(1:k-1) , * ) z(i)
    write( 13 , * ) x(i) , y(i) , z(i)
  End Do
  Close( 12 )
  Close( 13 )
End Program www_fcode_cn

Integer Function GetFileN( iFileUnit )
!// 此函数应在打开文件后立即调用。调用后读取位置返回文件起始位置
  Implicit None
  Integer , Intent( IN ) :: iFileUnit
  character( Len = 1 ) :: cDummy
  integer :: ierr
  GetFileN = 0
  Rewind( iFileUnit )
  Do
    Read( iFileUnit , * , ioStat = ierr ) cDummy
    If( ierr /= 0 ) Exit
    GetFileN = GetFileN + 1
  End Do
  Rewind( iFileUnit )
End Function GetFileN

作者: liux578    时间: 2015-8-18 21:55
百事可乐 发表于 2015-8-18 21:24
[mw_shl_code=fortran,true]Program www_fcode_cn
  Implicit None
  Integer :: i , k , ierr , N , GetFi ...

    Open(14,File="xyzn.txt")
    Open(13,File="gp_pos_new.txt")
    call GetFileN( 13 )
    write(14,'(1E11.4E2,2E12.4E3)')xyzn
    End Program Data_Processing

运行完了,xyzn.txt还是全部为0,不知道咋回事,下午弄的也全部是0,用你这个方法还都是0
作者: vvt    时间: 2015-8-18 22:51
你乱改代码当然结果不正确
作者: liux578    时间: 2015-8-19 08:26
vvt 发表于 2015-8-18 22:51
你乱改代码当然结果不正确

能明示么
作者: liux578    时间: 2015-8-19 08:28
liux578 发表于 2015-8-19 08:26
能明示么

我再好好看看
作者: liux578    时间: 2015-8-19 08:54
百事可乐 发表于 2015-8-18 21:24
[mw_shl_code=fortran,true]Program www_fcode_cn
  Implicit None
  Integer :: i , k , ierr , N , GetFi ...

群主,是我搞错了,多谢




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2