Fortran READ 到派生类型不适用于 gfortran(Fortran 运行时错误:文件结尾)

发布于 2025-01-13 21:58:55 字数 1756 浏览 0 评论 0原文

我在 Fortran 中有一个派生类型,我需要将文本文件中的值读入该类型。我的问题是我的代码在 Intel Fortran 编译器和 NAG Fortran 编译器上运行良好,但 GFortran 退出时出现错误。最小的工作示例如下。

Module my_mod
  Type T
    Real :: a
  End Type T

  Interface Read (Formatted)
    Module Procedure read_T
  End Interface
Contains
  Subroutine read_T(var, unit, iotype, v_list, iostat, iomsg)
    Class (T), Intent (Inout) :: var
    Integer, Intent (In) :: unit
    Character (*), Intent (In) :: iotype
    Integer, Intent (In) :: v_list(:)
    Integer, Intent (Out) :: iostat
    Character (*), Intent (Inout) :: iomsg

    Read (unit, *, iostat=iostat, iomsg=iomsg) var%a
  End Subroutine
End Module my_mod

Program main
  Use my_mod
  Implicit None
  Type(T) :: x
  Type(T) :: y

  Open(unit=20, file='data.txt', action='read')
  Read(20, *) x        ! Here GFortran fails because it somehow reaches EOF
  Write(*, *) x
  Read(20, *) y
  Write(*, *) y
End Program main

使用 data.txt 文件:

1.0
2.0

使用 GFortran 我收到错误

At line 30 of file test.f90 (unit = 20, file = 'data.txt')
Fortran runtime error: End of file

Error termination. Backtrace:
#0  0x7f27a76fef2f in finalize_transfer
        at ../.././libgfortran/io/transfer.c:4175
#1  0x400aca in ???
#2  0x400b86 in ???
#3  0x7f27a69a8504 in ???
#4  0x4007e8 in ???
#5  0xffffffffffffffff in ???

有效的一件事是替换 Read 语句,如下所示

Read(20, fmt='(DT)', advance='no') x        ! Works with all compilers :)
Write(*, *) x
Read(20, fmt='(DT)', advance='no') y        ! Works with all compilers :)
Write(*, *) y

所以我的问题..是否有可能在没有 fmt='(DT) 的情况下在 GFortran 中获得相同的行为',提前='否'

谁的行为是正确的? GFortran 还是英特尔 Fortran 和 NAG Fortran?

感谢您的帮助,

西蒙

I have a derived type in Fortran and I need to read values from a text file into that type. My problem is that my code is working fine with the Intel Fortran compiler as well as the NAG Fortran compiler, but GFortran exits with an error. The minimal working example is below.

Module my_mod
  Type T
    Real :: a
  End Type T

  Interface Read (Formatted)
    Module Procedure read_T
  End Interface
Contains
  Subroutine read_T(var, unit, iotype, v_list, iostat, iomsg)
    Class (T), Intent (Inout) :: var
    Integer, Intent (In) :: unit
    Character (*), Intent (In) :: iotype
    Integer, Intent (In) :: v_list(:)
    Integer, Intent (Out) :: iostat
    Character (*), Intent (Inout) :: iomsg

    Read (unit, *, iostat=iostat, iomsg=iomsg) var%a
  End Subroutine
End Module my_mod

Program main
  Use my_mod
  Implicit None
  Type(T) :: x
  Type(T) :: y

  Open(unit=20, file='data.txt', action='read')
  Read(20, *) x        ! Here GFortran fails because it somehow reaches EOF
  Write(*, *) x
  Read(20, *) y
  Write(*, *) y
End Program main

with the data.txt file:

1.0
2.0

With GFortran I get the error

At line 30 of file test.f90 (unit = 20, file = 'data.txt')
Fortran runtime error: End of file

Error termination. Backtrace:
#0  0x7f27a76fef2f in finalize_transfer
        at ../.././libgfortran/io/transfer.c:4175
#1  0x400aca in ???
#2  0x400b86 in ???
#3  0x7f27a69a8504 in ???
#4  0x4007e8 in ???
#5  0xffffffffffffffff in ???

One thing that works is to replace the Read statements as follows

Read(20, fmt='(DT)', advance='no') x        ! Works with all compilers :)
Write(*, *) x
Read(20, fmt='(DT)', advance='no') y        ! Works with all compilers :)
Write(*, *) y

So my question .. is it possible to get the same behaviour in GFortran without fmt='(DT)', advance='no'?

And who has the correct behavior? GFortran or Intel Fortran and NAG Fortran?

Thanks for any help,

Simon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文