简体   繁体   English

如何使用名称列表以派生类型编写可分配数组?

[英]How to write an allocatable array in a derived type using namelists?

I am having trouble writing an allocatable array nested in a derived type using namelists. 我在使用名称列表编写嵌套在派生类型中的可分配数组时遇到麻烦。 A minimal example is shown below. 一个最小的例子如下所示。 How can I modify the program to have the allocatable array inside the derived type work as though it were not nested? 我如何修改程序以使派生类型内部的可分配数组像未嵌套一样工作?

program test

    implicit none

    type struct_foo
        integer, allocatable :: nested_bar(:)
    end type struct_foo

    integer, allocatable :: bar(:)
    type(struct_foo) :: foo
    ! namelist / list / foo, bar
    namelist / list / bar

    allocate(bar(5))
    bar = [1:5]

    allocate(foo%nested_bar(5))
    foo%nested_bar=[1:5]

    write(*,list)

end program test

With the foo commented out of the namelist, it works just fine, producing the output: 将foo从名称列表中注释掉,它可以正常工作,并产生输出:

 &LIST
 BAR     =           1,           2,           3,           4,           5
 /

With foo included, the program fails to compile: 包含foo时,程序无法编译:

>> ifort -traceback test_1.f90 -o test && ./test
test_1.f90(20): error #5498: Allocatable or pointer derived-type fields require a user-defined I/O procedure.
    write(*,list)
--------^
compilation aborted for test_1.f90 (code 1)

As the error message states, you need to provide a user defined derived type I/O (UDDTIO) procedure. 如错误消息所述,您需要提供一个用户定义的派生类型I / O(UDDTIO)过程。 This is required for input/output of any object with an allocatable or pointer component. 对于具有可分配或指针组件的任何对象的输入/输出,这是必需的。

How the object of derived type is formatted in the file is completely under the control of the UDDTIO procedure. 如何在文件中格式化派生类型的对象的格式完全在UDDTIO过程的控制之下。

An example, using a very simple output format, is below. 下面是一个使用非常简单的输出格式的示例。 Typically a UDDTIO procedure implementing namelist output would use an output format that was consistent with the other aspects of namelist output and typically there would also be a corresponding UDDTIO procedure that was then able to read the formatted results back in. 通常,实现名称列表输出的UDDTIO过程将使用与名称列表输出的其他方面一致的输出格式,并且通常还将存在一个相应的UDDTIO过程,该过程随后能够读回格式化的结果。

module foo_mod
  implicit none

  type struct_foo
    integer, allocatable :: nested_bar(:)
  contains
    procedure, private :: write_formatted
    generic :: write(formatted) => write_formatted
  end type struct_foo
contains
  subroutine write_formatted(dtv, unit, iotype, v_list, iostat, iomsg)
    class(struct_foo), intent(in) :: dtv
    integer, intent(in) :: unit
    character(*), intent(in) :: iotype
    integer, intent(in) :: v_list(:)
    integer, intent(out) :: iostat
    character(*), intent(inout) :: iomsg

    integer :: i

    if (allocated(dtv%nested_bar)) then
      write (unit, "(l1,i10,i10)", iostat=iostat, iomsg=iomsg)   &
          .true.,  &
          lbound(dtv%nested_bar, 1),  &
          ubound(dtv%nested_bar, 1)
      if (iostat /= 0) return
      do i = 1, size(dtv%nested_bar)
        write (unit, "(i10)", iostat=iostat, iomsg=iomsg)  &
            dtv%nested_bar(i)
        if (iostat /= 0) return
      end do
      write (unit, "(/)", iostat=iostat, iomsg=iomsg)
    else
      write (unit, "(l1,/)", iostat=iostat, iomsg=iomsg) .false.
    end if
  end subroutine write_formatted
end module foo_mod

program test
  use foo_mod

  implicit none

  integer, allocatable :: bar(:)
  type(struct_foo) :: foo
  namelist / list / foo, bar

  allocate(bar(5))
  bar = [1:5]

  allocate(foo%nested_bar(5))
  foo%nested_bar=[1:5]

  write (*,list)
end program test

Use of UDDTIO obviously requires a compiler that implements this Fortran 2003 language feature. UDDTIO的使用显然需要实现此Fortran 2003语言功能的编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用带有名称列表的allocatable / assume-size数组读写 - Using allocatable/assumed-size arrays with namelist read write 有没有一种方法可以使用名称列表I / O功能读取具有可分配组件的派生类型? - Is there a way to use the namelist I/O feature to read in a derived type with allocatable components? 将可分配数组传递给子例程 - Passing allocatable array into a subroutine 使用 Fortran 将 txt 文件读入可分配数组时出现错误结果 90 - Wrong results when reading a txt file into an allocatable array using Fortran 90 Write语句不能在用户定义的格式化I / O过程中为派生类型生成新行 - Write statement cannot produce new lines within user-defined formatted I/O procedures for derived type 如何用C语言将数组写入文件 - How to write an array to file in C 如何在C ++中使用mmap将整数数组正确写入文件 - How can I write array of integers properly to file by using mmap in c++ 如何将数组内容写入硬盘 - how to write content of an array to hard disk 如何使用config.property文件将任何类型的文件(例如txt文件)写入资源文件夹,但不使用Java中的绝对路径文件 - How to write any type of file (for example txt file) to a resources folder with the config.property file, but without using absolute path file in java 如何使用StreamWriter写入文件? - How to Write to a file using StreamWriter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM