Problem reading from a file and storing in array
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
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.
精彩评论