fortran示例中的错误 - 分配内存错误1

时间:2014-01-27 15:30:45

标签: fortran

我是fortran的新手,我正在尝试编写和编译一个我在this address中找到的简单示例来读取一个简单的2列文本文件:

Date    Water-flow
717976  7.140
717977  6.570
717978  6.040
717979  5.780
717980  5.530

程序粘贴在下面,但是当我尝试编译并运行时,我真的不明白产生的错误,请你指教一下吗?

PROGRAM READHPOP
    IMPLICIT NONE
    INTEGER, PARAMETER  :: lun = 10
    INTEGER             :: res,i
    CHARACTER(len=80)   :: cbuffer
    INTEGER             :: flength
    INTEGER,ALLOCATABLE,DIMENSION(:)    :: dates
    REAL,ALLOCATABLE,DIMENSION(:)   :: water_flow
    INTEGER             :: c_position,string_length

    OPEN(UNIT=lun,FILE="st.dat",FORM="FORMATTED",IOSTAT=res)
    IF(res/=0) THEN
        PRINT*,'error in opening file, status: ',res
        STOP
    END IF
    READ(UNIT=lun,FMT='(A)',IOSTAT=res) cbuffer
    IF(res /=0) THEN
        PRINT *,'ERROR IN READING file, status: ',res
        CLOSE(UNIT=lun)
        STOP
    END IF
    string_length=LEN_TRIM(cbuffer)
    c_position=INDEX(cbuffer,':')
    READ(cbuffer(c_position+1:string_length),FMT='(A10)') flength
    ALLOCATE(dates(flength),STAT=res)
    IF (res/=0) THEN
        PRINT*,'ERROR IN ALLOCATING MEMORY, status:',res
        CLOSE(UNIT=lun)
        STOP
    END IF
    READ(UNIT=lun,FMT='(A)',IOSTAT=res) cbuffer
    DO i=1,cbuffer
        READ(UNIT=lun,FMT='(I6,1X,F6.3)') dates(i),water_flow(i)
        PRINT*,'DIAS ',dates(i)
    END DO
end PROGRAM readhpop
编辑:感谢所有输入,只是为了解决问题,我粘贴下面的工作代码,问候!

PROGRAM READHPOP
    IMPLICIT NONE
    INTEGER, PARAMETER  :: lun = 10
    INTEGER             :: res,i
    CHARACTER(len=80)   :: cbuffer
    INTEGER             :: flength
    INTEGER,ALLOCATABLE,DIMENSION(:)    :: dates
    REAL,ALLOCATABLE,DIMENSION(:)   :: water_flow
    INTEGER             :: c_position,string_length

    OPEN(UNIT=lun,FILE="st.dat",FORM="FORMATTED",IOSTAT=res)
    IF(res/=0) THEN
        PRINT*,'error in opening file, status: ',res
        STOP
    END IF

    READ(UNIT=lun,FMT='(A)',IOSTAT=res) cbuffer

    IF(res /=0) THEN
        PRINT *,'ERROR IN READING file, status: ',res
        CLOSE(UNIT=lun)
        STOP
    END IF

    string_length=LEN_TRIM(cbuffer)
    c_position=INDEX(cbuffer,':')

    READ(cbuffer(c_position+1:string_length),FMT='(I10)') flength
    ALLOCATE(dates(flength),water_flow(flength),STAT=res)
    IF (res/=0) THEN
    PRINT*,'ERROR IN ALLOCATING MEMORY, status:',res
        CLOSE(UNIT=lun)
        STOP
    END IF
    DO i=1,flength
        READ(UNIT=lun,FMT='(I6,F6.3)') dates(i),water_flow(i)
        PRINT*,'LINE OF FILE',i,'  DAYS ',dates(i),'  WATERFLOW ',water_flow(i)
    END DO
    PAUSE
END PROGRAM READHPOP

2 个答案:

答案 0 :(得分:3)

好的,这是一个工作版本:

PROGRAM READHPOP
    IMPLICIT NONE
    INTEGER, PARAMETER  :: lun = 10
    INTEGER             :: res,i
    CHARACTER(len=80)   :: cbuffer
    INTEGER             :: flength
    INTEGER,ALLOCATABLE,DIMENSION(:)    :: dates
    REAL,ALLOCATABLE,DIMENSION(:)   :: water_flow
    INTEGER             :: c_position,string_length

    OPEN(UNIT=lun,FILE="st.dat",FORM="FORMATTED",IOSTAT=res)
    IF(res/=0) THEN
        PRINT*,'error in opening file, status: ',res
        STOP
    END IF

    READ(UNIT=lun,FMT='(A)',IOSTAT=res) cbuffer

    IF(res /=0) THEN
        PRINT *,'ERROR IN READING file, status: ',res
        CLOSE(UNIT=lun)
        STOP
    END IF

    string_length=LEN_TRIM(cbuffer)
    c_position=INDEX(cbuffer,':')

    READ(cbuffer(c_position+1:string_length),FMT='(I10)') flength
    ALLOCATE(dates(flength),water_flow(flength),STAT=res)
    IF (res/=0) THEN
    PRINT*,'ERROR IN ALLOCATING MEMORY, status:',res
        CLOSE(UNIT=lun)
        STOP
    END IF

    DO i=1,flength
        READ(UNIT=lun,FMT='(I6,F6.3)') dates(i),water_flow(i)
        PRINT*,'DIAS ',dates(i)
    END DO
end PROGRAM readhpop

的问题:

  • water_flow需要分配
  • 确定flength时:flength是一个整数,因此请将其读作整数(此处为:'(I10)')。冒号后的日期数量,因此请将st.dat更改为:
Date Water-flow: 5
717976 7.140
717977 6.570
717978 6.040
717979 5.780
717980 5.530
  • 循环flength而不是cbuffer
  • 一个阅读声明太多 - 你试图超越文件的末尾......

答案 1 :(得分:2)

在这一行

    READ(cbuffer(c_position+1:string_length),FMT='(A10)') flength

您在字符编辑描述符下读取整数值。这可能会返回flength中的值,该值超出可分配数组的任何合理值。例如,在快速测试中,我得到了538981169的值。将该行更改为

    READ(cbuffer(c_position+1:string_length),'(i)') flength