如何在fortran中读取未知行数
我的数据文件是如何在fortran中读取未知行数,fortran,gfortran,Fortran,Gfortran,我的数据文件是 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
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
这回答了你的问题吗?倒带(3)这不是倒带(22)吗?请阅读。当你发布一些代码时,你应该问一些关于代码的问题。如果代码有问题,应该描述问题。是否有任何错误消息?还是结果错了?此外,对于Fortran问题,您应该使用tag。您可以将硬编码文件单位更改为
newunit
单位。应该尽可能使用Fortran2008标准。我甚至没有意识到你是OP!您知道您实际上可以编辑您的问题吗?如果(io/=0)退出
不一定表示文件结束错误。为此,有一个内在的Fortran函数,is_iostat_end(io)
,它返回值.true。
如果io
是与文件结束条件相对应的I/O状态值,则返回值.false。
否则返回值。
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