Fortran 运行时错误:浮点读取期间的错误值 (ES14.6E2)

问题描述 投票:0回答:1

我正在尝试执行 Fortran 代码,但出现以下错误:

在文件 src/annGeneralization.f90 的第 79 行(unit = 12, file = './output/ann1_1.out') Fortran 运行时错误:浮点读取期间的错误值

这就是我的 annGeneralization.f90 文件的第 77、78 和 79 行的样子:

        write(*,*) './output/ann' // trim(str1) // '_' // trim(str0) // '.out'
        open(12, FILE = './output/ann' // trim(str1) // '_' // trim(str0) // '.out')
        read(12, '(ES14.6E2)') fitness

看这里,我的输入文件格式有什么问题,但是我对fortran不是很有经验,我什么都试过了。我的 ann1_1.out 输入文件是:

fortran gfortran fortran90 fortran77 fortran95
1个回答
0
投票

如果您对此事有一些选择,那么我不会有输入文件,其中您想要的数据就在未知长度描述符之后的行尾。如果数据是记录中的第一件事,Fortran 会更容易。

但是,如果您对输入文件的格式感到困惑,那么您可以一次读取整行(为此您需要一个“a”格式说明符),找到分隔符的位置(“:”可以工作在您的情况下),将此分隔符后的实际字符子字符串(这在 Fortran 中称为“内部文件”)读入您的数据变量。

我认为这最好在一个单独的子程序中完成(遗憾的是,在没有模板程序的情况下,您将需要一个不同的用于整数数据的子程序)。

subroutine readParameter( unit, separator, description, value )
   implicit none
   integer, intent(in) :: unit                             
   character, intent(in) :: separator
   character(len=*), intent(out) :: description
   real, intent(out) :: value

   character(len=100) line
   integer i

   read( unit, "(a)" ) line                      ! Read a single line
   i = index( line, separator )                  ! Find the separator, here ":"
   description = line(1:i-1)                     ! The description comes before the sparator
   read( line(i+1:), * ) value                   ! The rest of the line contains your data

end subroutine readParameter


program test
   implicit none

   character(len=72) description
   real value
   character, parameter :: separator = ":"
   character(len=*), parameter :: filename = "ann1_1.out"
   character(len=*), parameter :: fmt = "( 'Description: ', a, /, 'Value: ', es12.5 )"

   open( 10, file=filename )
   call readParameter( 10, separator, description, value )
   write( *, fmt ) description, value
   close( 10 )

end program test

输出:

Description: Objective Function (MPCA)                                               
Value:  6.65359E-02
© www.soinside.com 2019 - 2024. All rights reserved.