Fortran:如何从文件中读取数组(Fortran: How to read to an array from a file)
我正在尝试从文件读取整数到数组。 但是当我运行程序时出现错误。
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 90I'm trying to read integers from a file to an array. But I get an error when I run the program.
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 MINTEMInput file looks like this:
22 100 90 80 70 60 100 90 80 70 60 100 90 80 70 60 100 90 80 70 100 90最满意答案
您提供的文件可以使用以下内容读入:
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)The file you provided can be read in using this:
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)更多推荐
发布评论