[Fortran] 纯文本查看 复制代码
program main
integer, allocatable :: elem(:)
c
call assem(elem)
c
end
c--------------------------------------------------------------------------
c--------------------------------------------------------------------------
subroutine assem(elem)
implicit none
character(Len=100) tmp_str(1000)
integer,allocatable :: elem(:)
integer I,J
open(11,file='data.txt',status=old)
c--------------------------------------------------------------------------
c put the whole file into memory as character ...starts
c--------------------------------------------------------------------------
J = 0
do I = 1,4000
read(11,'(a)',end=101) tmp_str(I)
J = J +1
101 enddo
c
allocate(elem(J))
c
c--------------------------------------------------------------------------
c put the whole file into memory as character ...completed
c--------------------------------------------------------------------------
c
c--------------------------------------------------------------------------
c put the data into array elem(:) ...starts
c--------------------------------------------------------------------------
c do I = 1,J
read( tmp_str(I),*)elem(I)
enddo
c--------------------------------------------------------------------------
c put the data into array elem(:) ...completed
c--------------------------------------------------------------------------
end
[Fortran] 纯文本查看 复制代码
Module DFile_Mod
Implicit None
contains
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
End Module DFile_Mod
Program main
use DFile_Mod
Implicit none
Integer, Allocatable :: elem(:)
Integer :: N
Open (11, File='data.txt', Status='old')
N = GetFileN( 11 )
Allocate( elem(N) )
Call assem( 11 , elem )
Write (*, *) elem
Deallocate( elem )
Close( 11 )
contains
Subroutine assem( FILEID , elem )
Integer :: elem(:) , FILEID , i
Do i = 1, size(elem)
Read ( FILEID , * ) elem(i)
End Do
End Subroutine assem
End Program main