使用read语句时出错

时间:2012-02-28 04:14:27

标签: compiler-errors fortran fortran95

我目前正在研究一个用于读取输入文件的FORTRAN程序。读取输入文件后,文件的第一行包含一个表示文件中矩阵数量的整数。第二行包含每个矩阵有多少行和列。其余的行包含矩阵本身。程序将读入每个矩阵并将其写入输出文件,并计算所有矩阵的总和。在读取矩阵数据时,我一直收到这两个错误

  

代码:READ(1,*,END = 99)(尺寸(I),I = 1,2)

     

错误:只读I / O列表中的常量和表达式无效。

     

错误2:此标签未定义[99]

我的代码是:

  *START OF CODE
  PROGRAM MAIN

  *DECLARATIONS
  INTEGER EXISTS, DONE, NUM, DIMENSIONS*2, USED
  INTEGER I,J, TEMPMAT(10,10), SUM(10,10),OVER
  CHARACTER INNAME*30, OUTNAME*30
  *INITIALIZATIONS
  INNAME='NULL.DOC'
  EXISTS=0
  DONE=0
  I=0
  J=0
  OVER=0
  USED=0
  NUM=1
  *FORMATS
  5     FORMAT(' ','SUM OF 'I2,X,'MATRICIES')
  *MAIN CODE
  *DO (0)

  *DO WHILE(1) INPUT NAME !EXIST AND != QUIT
   DO WHILE(EXISTS.EQ.0.OR.INNAME.EQ.'QUIT'.OR.INNAME.EQ.'Q')
  *PROMPT FOR NAME OF FILE AND INPUT
  PRINT *,'PLEASE INPUT A FILE NAME INCLUDING AN EXTENSION'
  PRINT *,'ENTER QUIT OR Q TO EXIT'
  READ (*,*) INNAME

  *TEST FOR FILE
  INQUIRE(FILE=INNAME, EXIST=EXISTS)
  PRINT *, 'INPUT FILE EXSISTS'
  *IF EXIST=FALSE, DISPLAY MESSAGE 
  IF(.NOT.EXISTS) THEN
  PRINT *,'FILE DOES NOT EXIST, PLEASE TRY AGAIN'

  *ELSE EXIST=TRUE, OPEN (UNIT=1)
  ELSE 
  OPEN(UNIT=1, FILE=INNAME, STATUS='OLD')
  END IF
  *END DO(1)      
  END DO
  *ASK CHOICES FOR OUTPUT
  PRINT *,'PLEASE ENTER A NAME FOR AN OUTPUT FILE W/ EXTENSION'
  PRINT *,'ENTER QUIT OR Q TO EXIT'
  READ(*,*)OUTNAME

  IF(OUTNAME.EQ.'Q'.OR.OUTNAME.EQ.'QUIT')THEN
    DONE=1
  END IF
  *CHECK IF DONE (IF 1)
  IF(DONE.EQ.0)THEN
  *OPEN THE OUTPUT FILE DO(2)
        DO WHILE(USED.EQ.0)
            INQUIRE(FILE=OUTNAME,EXIST=EXISTS)
            PRINT *, 'OUTPUT FILE EXSITS'
  *OPEN IF FOR IF FILE ALREADY EXISTS
            IF(EXISTS.EQ.1)THEN
                DO WHILE(OVER.LT.1.OR.OVER.GT.2)
                PRINT *,'FILE ALREADY EXISTS. DO YOU WANT TO OVERWRITE?'
                PRINT *,'SELECT 1 FOR YES OR 2 FOR NO'
                SELECT CASE(OVER)
                CASE(1)
                    OPEN(UNIT=2,FILE=OUTNAME,STATUS='OLD')
                    USED=1            
                CASE(2)
  *DO NOTHING  
                CASE DEFAULT
                    PRINT *, 'THAT WAS NOT EVEN AN VALID INPUT, TRY AGAIN'
  *END SELECT
                END SELECT 
  *END IF FOR IF FILE ALREADY EXISTS AMD OPEN IF DID NOT EXIST, END DO(2)
                END DO
            END IF
            OPEN(UNIT=2,FILE=OUTNAME,STATUS='NEW')                   
  *READ IN 1 INTEGER (MAXIMUM VALUE OF 10) FOR # OF MATRICIES IN FILE
            READ(1,*) NUM
            PRINT *, 'THE NUMBER OF MATRICIES: ', NUM
  *READ IN 2 INTEGERS, 1=ROWS, 2=COLUMNS MAX OF 10 FOR EACH
            READ(1,*,END=99)(DIMENSIONS(I), I=1,2)
  *DO WHILE MORE MATRICIES EXIST DO(3)
            DO WHILE(NUM.GT.0)
  *READ THE MATRIX IN DO(4)
                DO J=1,DIMENSIONS(1)
                    READ(1,*,END=99)(TEMPMAT(J,I),I=1,DIMENSIONS(2))
  *END DO (4)
                END DO   
  *ADD MATRIX TO SUM MATRIX
  *DO(5)(6)
                DO J=1,10
                    DO I=1,10
                        SUM(J,I)=SUM(J,I)+TEMPMAT(J,I)
  *END DO(5)(6) 
                    END DO
                END DO
  *PRINT CURRENT MATRIX TO FILE W/ HEADER 'MATRIX X'
  *DO(6)
                DO J=1,DIMENSIONS(1)
                    WRITE(2,*)(TEMPMAT(J,I),I=1,DIMENSIONS(2))
  *END DO(6)
                END DO  
  *WRITE HEADING 'SUM OF ALL N MATRICES'
                WRITE(2,*)5,NUM
                NUM=NUM-1
  *END DO(3)
            END DO
  *END DO(0)
  END DO
  *PRINT SUM OF MATRICES
  *DO(7)
  DO J=1,10
    WRITE(2,*)(SUM(J,I),I=1,10)
  *END DO(7)
  END DO 
  END IF
  *STOP
  STOP
  *END
  END

2 个答案:

答案 0 :(得分:1)

呃,我想它想在EOF上去“99”。

你还没有定义第99行,对吗?

建议:尝试加入“99 CONTINUE”行。

此链接也可能有所帮助:

答案 1 :(得分:1)

尺寸以特殊方式声明。尝试尺寸(2)。也许这与它有关,因为带有错误消息的行是第一个使用维度。