从文件读取并存储在数组中时出现问题

发布于 2024-11-01 19:48:58 字数 775 浏览 7 评论 0原文

使用 g96 编译器时,我收到一条错误消息:

INTENT(OUT) at variable 'SIZE' is never set.

下面是我的子例程。你知道我该如何修复这个错误吗?非常感谢!

SUBROUTINE getFileItems(size,itemarray,pricearray,quantityarray)

INTEGER:: iost=0, i=0
INTEGER, INTENT(OUT):: quantityarray(50)
INTEGER, INTENT(OUT):: size
REAL, INTENT(OUT):: pricearray(50)
CHARACTER(20),INTENT(OUT)::itemarray(50)
CHARACTER(20)::namefiletoread

PRINT*,"Enter the name of file you would like to read: "
READ*,namefiletoread

OPEN(UNIT=44,FILE = namefiletoread, ACTION = "READ", !POSITION="REWIND",IOSTAT=iost)
IF(iost>0)STOP "Problem opening the file!"

DO i=1, size
READ(44,'(A,F6.2,I8)',IOSTAT=iost), itemarray(i), pricearray(i),quantityarray(i)
IF(iost<0)STOP
END DO


END SUBROUTINE

Using the g96 compiler, I got an error saying:

INTENT(OUT) at variable 'SIZE' is never set.

Below is my subroutine. Do you know how I can fix this error? Thanks so much!

SUBROUTINE getFileItems(size,itemarray,pricearray,quantityarray)

INTEGER:: iost=0, i=0
INTEGER, INTENT(OUT):: quantityarray(50)
INTEGER, INTENT(OUT):: size
REAL, INTENT(OUT):: pricearray(50)
CHARACTER(20),INTENT(OUT)::itemarray(50)
CHARACTER(20)::namefiletoread

PRINT*,"Enter the name of file you would like to read: "
READ*,namefiletoread

OPEN(UNIT=44,FILE = namefiletoread, ACTION = "READ", !POSITION="REWIND",IOSTAT=iost)
IF(iost>0)STOP "Problem opening the file!"

DO i=1, size
READ(44,'(A,F6.2,I8)',IOSTAT=iost), itemarray(i), pricearray(i),quantityarray(i)
IF(iost<0)STOP
END DO


END SUBROUTINE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所谓喜欢 2024-11-08 19:48:59

您需要以某种方式初始化“size”的值。几种可能的方法:
1) 如果子例程外部已知大小,则使 sizeintent(in) 并在调用例程中设置该值,
2)提示用户输入值,
3)获取文件第一行的数组长度并读取它。
4) 如果文件的项目数可能可变,请读取它直到遇到 EOF,计算项目数。使用无限循环并在到达 EOF 时退出,将大小设置为读取的项目数。

You need to initialize the value of "size" somehow. Several possible methods:
1) If the size is known externally to the subroutine, make size intent(in) and set the value in the calling routine,
2) Prompt the user for the value,
3) Have the length of the array on the first line of the file and read it.
4) If the file may have a variable number of items, read it until you hit EOF, counting the number of items. Use an infinite loop and exit when you hit the EOF, setting size to the number of items read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文