从名称列表中读取可分配数组

Ant*_*ano 5 fortran configuration-files allocatable-array

我正在使用GNU Fortran(GCC)4.8.2

我想从名称列表中读取可分配数组。但是我事先不知道必须向可分配数组中读取多少个元素,因此我无法在读取名称列表之前对其进行分配。

这是我的名单:namelist.nml:

&SECTION_1
    intList = 5,6,7
&END
Run Code Online (Sandbox Code Playgroud)

这是我的程序:namelist.f08:

program namelist
    implicit none

    integer, allocatable    :: intList(:)
    integer                 :: U    ! Unit to read the namelist file

    namelist /SECTION_1/ intList

    !allocate(intList(3)) ! <-- If I uncomment this, the program works.
    open(NEWUNIT=U, file="namelist.nml", status='OLD', recl=80, delim='APOSTROPHE')
    rewind(U)
    read(U, nml=SECTION_1)
    close(U)

    write (*,*) intList
end program namelist
Run Code Online (Sandbox Code Playgroud)

如果我取消标记行的注释,程序将运行,但是,正如我之前说的,我无法在分配之前读取名称列表。有人知道如何做到这一点吗?

Vla*_*r F 1

在阅读未来的 Fortran 标准时,有人非正式地呼吁提出自动分配的建议。至少对于字符数据能够读取未知长度的文本行https://github.com/j3-fortran/fortran_proposals/issues/9存在自动分配I/O 语句中的IOMSG或说明符的实际建议https: //j3-fortran.org/doc/year/19/19-252.txtERRMSG

但据我所知,一切还没有完成。当前的 Fortran 中没有自动解决方案。

读取之前必须分配到足够大的大小。没有其他办法。还有其他读取数据的方法,还有自定义数据格式和自定义解析器。名单并不代表一切,尽管名单通常很方便。