Fortran 中 de/allocate 的问题

时间:2020-12-24 18:01:51

标签: memory-management io fortran

我在循环中取消/分配以增加数组的大小(从文本文件输入)。问题是我打印时只得到最后几个值。怎么了?

代码如下:

program prueba
    implicit none
    integer, dimension(:), allocatable :: minuto
    integer :: iounit, ierr
    integer :: i = 1
    integer :: n = 1

    open(newunit = iounit, file = 'datos.txt')
    read(iounit,*)

    allocate(minuto(n))
    minuto = 0
    !-------------------------------------------

    do
        read(unit = iounit, fmt = '(i2)',iostat = ierr) minuto(i)
        if (ierr/=0) exit
        i = i+1
        n = i
        deallocate(minuto)
        allocate(minuto(n))
    end do
    deallocate(minuto)
    allocate(minuto(n-1))

    close(iounit)

print*,minuto

end program prueba

这是txt内容:

minutos
 5
10
15
20
25
30
35
40
45
50
55
 0

结果如下:

 -2144186072           1 -2144186072           1           0           0          35          40          45          50          55           0

0 个答案:

没有答案
相关问题