从文件读取并存储在数组中时出现问题
使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要以某种方式初始化“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.