读取来自纳默斯主义者的可分配阵列



我正在使用gnu fortran(gcc)4.8.2

我想阅读Namelist的可分配阵列。但是我不知道必须在可分配的数组中读取多少个元素,所以我不能在阅读Namelist之前分配它。

这是我的纳默斯特:namelist.nml:

&SECTION_1
    intList = 5,6,7
/ ! End of SECTION_1

这是我的程序: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

如果我不使用标记的行,则该程序有效,但是,正如我之前说的,我不能在阅读Namelist之前分配。有人知道如何完成此操作吗?

在阅读未来fortran标准时,有非正式的要求自动分配的建议。至少要使字符数据能够读取未知长度的文本行https://github.com/j3-fortran/fortran_proposals/issues/9存在I/O语句中IOMSGERRMSG规格的自动分配的实际建议https://j3-fortran.org/doc/year/19/19-252.txt

但是什么都没结束,阿法克。当前fortran中没有自动解决方案。

阅读之前,您必须分配足够大的尺寸。没有其他方法。还有其他方法可以读取数据,并且还有自定义数据格式和自定义解析器。即使经常方便,纳分师也不是一切。

相关内容

最新更新