读取文本文件时出现文件结束错误

时间:2016-02-14 13:04:04

标签: matrix input fortran readfile

我在运行吹扫程序时遇到问题,该程序用于读取文本文件作为输入文件。此输入文件类似于具有3列的矩阵 - 3个格式为(3(F3.6,1x))的数字 - 和4368行。

输入文件是:

  USE BIEF
  USE DECLARATIONS_TELEMAC2D
  IMPLICIT NONE
  INTEGER LNG,LU, ITRAC,I, NSOM,J, K, NDOWN
  INTEGER, PARAMETER :: NLINE =4368
  DOUBLE PRECISION, PARAMETER:: BATHY_RADIER_up= 29.84D0
  DOUBLE PRECISION, PARAMETER:: DEPTH_up = 2.15D0
  REAL :: A(5000),B(5000),C(5000)
  DOUBLE PRECISION :: XPOLYD(14), YPOLYD(14), INPOLYD(14)
  COMMON/INFO/LNG,LU

  DOUBLE PRECISION XPOLY(6), YPOLY(6),COTE_RADIER_up


   NSOM = 6
   XPOLY(1) = 602883.13
   XPOLY(2) = 602886.15
   XPOLY(3) = 602887.15
   XPOLY(4) = 602905.46
   XPOLY(5) = 602902.52
   XPOLY(6) = 602884.13


   YPOLY(1) = 128779.99
   YPOLY(2) = 128780.80
   YPOLY(3) = 128777.12
   YPOLY(4) = 128741.21
   YPOLY(5) = 128739.75
   YPOLY(6) = 128775.96



  AT = 0.D0


  CALL OS( 'X=0     ' , X=U )
  CALL OS( 'X=0     ' , X=V )


  IF(CDTINI(1:10).EQ.'COTE NULLE'.OR.
 *   CDTINI(1:14).EQ.'ZERO ELEVATION') THEN
    CALL OS( 'X=C     ' , H , H  , H , 0.D0 )
    CALL OS( 'X=X-Y   ' , H , ZF , H , 0.D0 )
  ELSEIF(CDTINI(1:14).EQ.'COTE CONSTANTE'.OR.
 *       CDTINI(1:18).EQ.'CONSTANT ELEVATION') THEN
    CALL OS( 'X=C     ' , H , H  , H , COTINI )
    CALL OS( 'X=X-Y   ' , H , ZF , H , 0.D0   )
  ELSEIF(CDTINI(1:13).EQ.'HAUTEUR NULLE'.OR.
 *       CDTINI(1:10).EQ.'ZERO DEPTH') THEN
    CALL OS( 'X=C     ' , H , H  , H , 0.D0  )
  ELSEIF(CDTINI(1:17).EQ.'HAUTEUR CONSTANTE'.OR.
 *       CDTINI(1:14).EQ.'CONSTANT DEPTH') THEN
    CALL OS( 'X=C     ' , H , H  , H , HAUTIN )
  ELSEIF(CDTINI(1:13).EQ.'PARTICULIERES'.OR.
 *       CDTINI(1:10).EQ.'PARTICULAR'.OR.
 *       CDTINI(1:07).EQ.'SPECIAL') THEN


    NDOWN = 14

  XPOLYD(1) = 602883.13
  XPOLYD(2) = 602886.15
  XPOLYD(3) = 602864.47
  XPOLYD(4) = 602837.90
  XPOLYD(5) = 602821.91
  XPOLYD(6) = 602649.77
  XPOLYD(7) = 602634.35
  XPOLYD(8) = 602345.08
  XPOLYD(9) = 602326.07
  XPOLYD(10) = 602619.31
  XPOLYD(11) = 602638.33
  XPOLYD(12) = 602811.64
  XPOLYD(13) = 602831.52
  XPOLYD(14) = 602857.16


  YPOLYD(1) = 128779.99
  YPOLYD(2) = 128780.80
  YPOLYD(3) = 128867.74
  YPOLYD(4) = 128936.74
  YPOLYD(5) = 128953.95
  YPOLYD(6) = 129105.43  
  YPOLYD(7) = 129143.43
  YPOLYD(8) = 129713.38
  YPOLYD(9) = 129708.26
  YPOLYD(10) = 129136.41
  YPOLYD(11) = 129094.72 
  YPOLYD(12) = 128941.16      
  YPOLYD(13) = 128931.09
  YPOLYD(14) = 128865.81

  PRINT *, 'opening file'
  DO 10 J=1,NPOIN 
   IF(INPOLY(X(J),Y(J),XPOLY,YPOLY,NSOM)) THEN
   PRINT *, 'upstream area'
      H%R(J)=MAX(0.D0,COTE_RADIER_up-ZF%R(J))
      U%R(J)=0.0D0
      PRINT *, 'upstream area'
      write(lu,*) 'upstream ....',J,H%r(J)

   ELSE
    IF(INPOLY(X(J),Y(J),XPOLYD,YPOLYD,NDOWN)) THEN
     OPEN(unit =90, FILE = 'cunnette_xyz.txt', FORM='FORMATTED')
     PRINT *, 'downstream area'
     READ(90,*) A(K),B(K),C(K)
     PRINT *, 'already read'
      DO K=1,NLINE
     PRINT *, "number of lines read:", NLINE
        IF(A(K).EQ.X(J).AND.B(K).EQ.Y(J)) then
          PRINT *, 'Nodes are inside'
          H%R(K)=0.45D0
          U%R(K)=0.D0
        ELSE
          H%R(K)=0.0D0
          U%R(K)=0.0D0
        ENDIF
      ENDDO
     CLOSE(90)
     ENDIF
   ENDIF

10    CONTINUE

  ELSE
    IF(LNG.EQ.1) THEN
    WRITE(LU,*) 'CONDIN : CONDITION INITIALE NON PREVUE : ',CDTINI
    ENDIF
    IF(LNG.EQ.2) THEN
    WRITE(LU,*) 'CONDIN: INITIAL CONDITION UNKNOWN: ',CDTINI
    ENDIF
    STOP
  ENDIF

  IF(NTRAC.GT.0) THEN
    DO ITRAC=1,NTRAC
      CALL OS( 'X=C     ' , X=T%ADR(ITRAC)%P , C=TRAC0(ITRAC) )
    ENDDO
  ENDIF

  CALL OS( 'X=C     ' , VISC , VISC , VISC , PROPNU )

  RETURN
  END           

计划是:

class ExampleMailer < ApplicationMailer
  default from: "philotester5@gmail.com"

  def sample_email(user)
    @user = user


    mg_client = Mailgun::Client.new ENV['api_key']
    message_params = {:from    => ENV['gmail_username'],
                      :to      => @user.email,
                      :subject => 'Sample Mail using Mailgun API',
                      :text    => 'This mail is sent using Mailgun API via mailgun-ruby'}
    mg_client.send_message ENV['domain'], message_params

  end
end

运行时的错误消息是:

  

在行读取文件:Fortran运行时错误:文件结束。

我的最后一个输出是“下游区域”aftre open命令。请问有人帮帮我吗?

1 个答案:

答案 0 :(得分:0)

首先,您要为J的每个值重复打开文件。这似乎不对。

其次,你没有给变量NPOIN一个值(大于1),所以J上的外循环不会停止运行(我猜),这最终会导致你试图超越文件的末尾阅读。

你有嵌套循环,一个在J上,一个在K上,这似乎不合逻辑。你应该只有一个。

J循环之外打开文件,然后每行读取一行,直到到达文件末尾:

INTEGER :: Reason
INTEGER :: NPOIN

OPEN(unit =90, IOSTAT=Reason, FILE = 'cunnette_xyz.txt', FORM='FORMATTED')
IF (Reason > 0)  THEN
      * ... something wrong ...
      PRINT *, 'Error opening file'
      STOP
END IF
NPOIN = 0
DO
   NPOIN = NPOIN + 1
   READ(*,*, IOSTAT=Reason) A(NPOIN), B(NPOIN), C(NPOIN)
   IF (Reason > 0)  THEN
      PRINT *, 'Error reading input from file. Aborted'
      STOP
   ELSE IF (Reason < 0) THEN
      * ... end of file reached ...
      NPOIN = NPOIN - 1
      PRINT *, 'All data read from file'
      EXIT
   END IF
END DO
* All input should be in A, B, C arrays now, with NPOIN entries

DO K=1, NPOIN
    * Your processing comes here. No more file I/O.
END DO