Fortran Coder

查看: 13884|回复: 10
打印 上一主题 下一主题

[文件读写] 求助一个文件读写的问题

[复制链接]

100

帖子

0

主题

0

精华

专家

F 币
550 元
贡献
291 点

规矩勋章元老勋章

QQ
楼主
发表于 2015-8-18 21:24:44 | 显示全部楼层
[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 
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-17 00:13

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表