Fortran:如何从文件中读取数组

时间:2012-11-06 05:25:07

标签: io fortran fortran90 fortran77 fortran95

我正在尝试从文件到数组读取整数。但是当我运行程序时出现错误。

PROGRAM MINTEM

INTEGER TEMP(4,7), I, J, MINIMUM, CURRENT

OPEN(UNIT=1, FILE='temps.dat')
READ (1,*) ((TEMP(I,J),J=1,7),I=1,4)

MINIMUM = TEMP(1,1)
DO I = 1,4
    DO J = 1,7
        IF (TEMP(I,J) < MINIMUM) THEN
            MINIMUM = TEMP(I,J)
        END IF
    END DO
END DO


PRINT *, "MINIMUM TEMPERATURE = ", MINIMUM
END PROGRAM MINTEM

输入文件如下所示:

 22
100 90 80 70 60 100 90 80 70 60 100 90 80 70 60 100 90 80 70
100 90

1 个答案:

答案 0 :(得分:2)

您提供的文件可以使用以下内容读取:

integer, allocatable :: t(:)
open(1,file='temp.dat')
read(1,*) N   ! your first line with 22
allocate( t(N-1) )  ! further on you only have 21 elements
read(1,*)t          ! so, read them in 
print*, t
deallocate(t)
close(1)