如何在 Fortran 中读取未知行数
How to read unknown number of rows in fortran
implicit none
real, dimension(:) ,allocatable :: t1, t2, t3
real :: t4, t5, t6
integer:: i, n ,io
CHARACTER(LEN=30) :: Format
Format ="(3X,7F8.2)"
open (unit=22,file='non-pol-0mm-300-conf-x-density.dat',status = 'old', action = 'read')
open (unit=50,file='non-pol-0mm-300-conf-x-fin-den.dat',status='unknown')
t4 = 1.66054
t5 = 2782.70
n = 0
DO
READ(22,iostat=io)
IF (io/=0) EXIT
n = n + 1
END DO
print*, n
allocate( t1(n) ,t2(n), t3(n) )
rewind(3)
DO i =1,n
READ(22,*) t1(i), t2(i), t3(i)
END DO
do i=1,n
t6(i) = (t2(i)* t4) / t5
end do
do i=1,n
write(50,Format) t1(i),t6(i)
end do
stop
end
我的数据文件是
-27.7500 0.0000 0.0000
-27.2500 0.8333 2.3407
-26.7500 99.7305 21.9321
-26.2500 123.1351 26.8580
-25.7500 172.4804 35.9525
-25.2500 239.6032 44.6065
-24.7500 279.7892 43.8637
-24.2500 390.2245 45.5373
-23.7500 452.6671 81.7495
-23.2500 525.5753 67.6686
-22.7500 545.1488 60.7696
-22.2500 589.7524 49.3679
-21.7500 617.3149 38.4744
-21.2500 638.5726 39.6387
这是正确的代码。
program densityplot
implicit none
real, dimension(:) ,allocatable :: t1, t2, t3
real :: t4, t5
integer:: i, n ,io
CHARACTER(LEN=30) :: Format
Format ="(3X,7F8.3)"
open (unit=22,file='non-pol-0mm-300-conf-x-density.dat',status = 'old', action = 'read')
open (unit=50,file='non-pol-0mm-300-conf-x-fin-den.dat',status='unknown')
n = 0
DO
READ(22,*,iostat=io)
IF (io/=0) EXIT
n = n + 1
END DO
! print*, n
allocate( t1(n) , t2(n), t3(n) )
rewind(22)
DO i =1,n
READ(22,*) t1(i), t2(i), t3(i)
END DO
t4 = 1.66054
t5 = 2782.70
do i=1,n
write(50,Format) t1(i),(t2(i)*t4)/t5
end do
end program densityplot
implicit none
real, dimension(:) ,allocatable :: t1, t2, t3
real :: t4, t5, t6
integer:: i, n ,io
CHARACTER(LEN=30) :: Format
Format ="(3X,7F8.2)"
open (unit=22,file='non-pol-0mm-300-conf-x-density.dat',status = 'old', action = 'read')
open (unit=50,file='non-pol-0mm-300-conf-x-fin-den.dat',status='unknown')
t4 = 1.66054
t5 = 2782.70
n = 0
DO
READ(22,iostat=io)
IF (io/=0) EXIT
n = n + 1
END DO
print*, n
allocate( t1(n) ,t2(n), t3(n) )
rewind(3)
DO i =1,n
READ(22,*) t1(i), t2(i), t3(i)
END DO
do i=1,n
t6(i) = (t2(i)* t4) / t5
end do
do i=1,n
write(50,Format) t1(i),t6(i)
end do
stop
end
我的数据文件是
-27.7500 0.0000 0.0000
-27.2500 0.8333 2.3407
-26.7500 99.7305 21.9321
-26.2500 123.1351 26.8580
-25.7500 172.4804 35.9525
-25.2500 239.6032 44.6065
-24.7500 279.7892 43.8637
-24.2500 390.2245 45.5373
-23.7500 452.6671 81.7495
-23.2500 525.5753 67.6686
-22.7500 545.1488 60.7696
-22.2500 589.7524 49.3679
-21.7500 617.3149 38.4744
-21.2500 638.5726 39.6387
这是正确的代码。
program densityplot
implicit none
real, dimension(:) ,allocatable :: t1, t2, t3
real :: t4, t5
integer:: i, n ,io
CHARACTER(LEN=30) :: Format
Format ="(3X,7F8.3)"
open (unit=22,file='non-pol-0mm-300-conf-x-density.dat',status = 'old', action = 'read')
open (unit=50,file='non-pol-0mm-300-conf-x-fin-den.dat',status='unknown')
n = 0
DO
READ(22,*,iostat=io)
IF (io/=0) EXIT
n = n + 1
END DO
! print*, n
allocate( t1(n) , t2(n), t3(n) )
rewind(22)
DO i =1,n
READ(22,*) t1(i), t2(i), t3(i)
END DO
t4 = 1.66054
t5 = 2782.70
do i=1,n
write(50,Format) t1(i),(t2(i)*t4)/t5
end do
end program densityplot