我正在尝试解决与保留值相关的问题,当我在下面显示的代码(fortran)中使用 de/allocate 时,制作了一个复制数组,但问题仍然存在。我已经看到与该主题相关的链接:
如果我知道数组维度(来自 txt 文件的输入),这将很容易并且没有意义(出于此代码的目的)。
可能我犯了一些错误(其中一个很明显:分钟尺寸与预期的总尺寸)。如果有人指定它们,我将不胜感激。尽管如此,我还是不明白如何制作一个复制数组来解决这个问题,因为我需要取消/分配临时变量和主变量。
那么,是否可以使用重新分配(取消/分配)来读取没有“可变维度”信息的 txt?
这就是代码(使用 f90):
program prueba
implicit none
integer, dimension(:), allocatable :: minuto, temp
integer :: iounit, ierr
integer :: i = 1
integer :: n = 1
open(newunit = iounit, file = 'datos.txt')
read(iounit,*)
allocate(minuto(n), temp(n))
minuto = 0; temp = 0
!-------------------------------------------
do
read(unit = iounit, fmt = '(i2)',iostat = ierr) temp(i)
if (ierr/=0) exit
if (mod(size(temp,1),5)==0) then
deallocate(minuto)
allocate(minuto(i))
minuto((i-4):i) = temp((i-4):i)
end if
i = i+1
deallocate(temp)
allocate(temp(i))
end do
close(iounit)
print*,minuto
end program prueba
(我知道实现相同目标的更好方法,这只是加深练习)
我使用这个数据示例(来自 txt):
min
5
10
15
20
25
30
35
40
45
50
55
0
结果是这样的:
-2144186072 1 -2144186072 1 25 0 35 40 45 50