Fortran:如何从文件中读取数组(Fortran: How to read to an array from a file)

编程入门 行业动态 更新时间:2024-10-08 22:21:28
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 90

I'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 MINTEM

Input 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)

更多推荐

本文发布于:2023-07-19 15:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1182556.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   文件   Fortran   file   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!