liux578 发表于 2015-8-17 20:27:01

求助一个文件读写的问题

如图所示的数据文件,我想用fortran把X Y Z对应下面的数据存入到一个数组中和写入到一个文件中,要求只存X Y Z的数据(坐标),其他的不要,请问该怎么操作?多谢指导

楚香饭 发表于 2015-8-17 20:35:56

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:43

非常感谢

liux578 发表于 2015-8-18 20:44:49

群主,请问怎样把读出来的数据赋值给一个数组啊

liux578 发表于 2015-8-18 20:45:09

楚香饭 发表于 2015-8-17 20:35
Program www_fcode_cn
Implicit None
Integer :: i , ierr


群主,请问怎样把读出来的数据赋值给一个数组啊

百事可乐 发表于 2015-8-18 21:24:44

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:30

百事可乐 发表于 2015-8-18 21:24
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:52

你乱改代码当然结果不正确

liux578 发表于 2015-8-19 08:26:48

vvt 发表于 2015-8-18 22:51
你乱改代码当然结果不正确

能明示么

liux578 发表于 2015-8-19 08:28:46

liux578 发表于 2015-8-19 08:26
能明示么

我再好好看看
页: [1] 2
查看完整版本: 求助一个文件读写的问题