[Fortran] 纯文本查看 复制代码
module util
contains
subroutine loclabel(fileid,label,ifound,irewind)
integer fileid,error
integer,optional :: ifound,irewind
character(len=80) :: c80
CHARACTER(LEN=*) label
if ((.not.present(irewind)).or.(present(irewind).and.irewind==1)) rewind(fileid)
do while(.true.)
read(fileid,"(a80)",iostat=ierror) c80
if (index(c80,label)/=0) then
backspace(fileid)
if (present(ifound)) ifound=1
return
end if
if (ierror/=0) exit
end do
if (present(ifound)) ifound=0
end subroutine
end module
program formout
!implicit none
use util
character infilename*200,outfilename*200,ord*8,findtraj*27
integer ntraj,nmax,i,ntmp,j,k,l
logical alive
integer(4),allocatable :: nnum(:)
character(8),allocatable :: bond(:,:)
write(*,*) "Input the .txt filename to be loaded"
do while(.true.)
read(*,"(a)") infilename
inquire(file=infilename,exist=alive)
if (alive) exit
write(*,*) "Cannot found this file, input again"
end do
open(10,file=infilename,status="old")
call loclabel(10,'Trajectory MaxJob')
read(10,"(49x,i12)") ntraj
write(*,"('Number of trajs:',i10)") ntraj
allocate(nnum(ntraj))
nnum=0
do i=1,ntraj
write(findtraj,"(a,i8,a)") 'Traj num',i,' Geometries'
call loclabel(10,findtraj,ifound)
read(10,"(49x,i12)") ntmp
nnum(i)=ntmp
end do
nmax=MaxVal(nnum)
write(*,"('The max step in some traj :',i10)") nmax
close(10)
allocate(bond(nmax,(ntraj+1)))
bond=' '
do i=1,nmax
write(ord,"(i8)") i
bond(i,1)=ord
end do
l=1
do while (l <= ntraj)
open(10,file=infilename,status="old")
write(findtraj,"(a,i8,a)") 'Traj num',l,' Geometries'
call loclabel(10,findtraj,ifound)
read(10,"(49x,i12)") ntmp
l=l+1
!REWIND(10)
!nnum(i)
do k=1,ntmp
read(10,'(a8,$)') bond(k,l) !为了不换行
end do
write(*,*) bond
end do
write(*,*) "aaaaa"
write(*,*) bond
close(10)
print*, "4.0"
write(*,*) "Input the .txt filename to be outputted"
read(*,"(a)") outfilename
open(10,file=outfilename,status="replace")
do i=1,nmax
do j=1,(ntraj+1)
write(10,'(a10,$)') bond(i,j)
end do
write(10,"(/)")
end do
close(10)
pause
end program formout