首页 文章

写作时出现“Fortran运行时错误:文件结束”

提问于
浏览
0

我编写了一段代码,用Arch Linux上的GNU Fortran(GCC)7.2.1 20171128编译,试图写入文件 . 该单元使用 newunit=... Fortran 2008功能打开

尝试写入文件时,代码崩溃,引发错误 Fortran runtime error: End of file .

非工作代码

这是代码的最小非工作版本 . 如果该文件不存在,代码将与gfortran 7.2.1崩溃

program foo
  implicit none

  character(len=80) :: filename
  character(len=5) :: nchar

  integer :: ilun=1

  call title(1, nchar)
  ! nchar = '00001'

  filename = trim(nchar)//'.txt'

  write(*, '(a, "<", a, ">")') 'filename ', trim(filename)
  open(newunit=ilun, file=trim(filename), form='formatted', status='replace')
  write(ilun, '(a1,a12,a10)') '#', 'Family', 'Count'
  close(ilun)
end program foo

subroutine title(n, nchar)
  implicit none
  integer, intent(in) :: n
  character(len=5), intent(out) :: nchar
  write(nchar, '(i0.5)') n

end subroutine title

这里的命令我正在使用 rm -f 00001.txt; gfortran foo.f90 -o a.out && ./a.out .

工作代码

相比之下,以下代码在同一台机器上编译并完美运行

程序foo

implicit none

  character(len=80) :: filename
  character(len=5) :: nchar

  integer :: ilun=1

  ! call title(1, nchar)
  nchar = '00001'

  filename = trim(nchar)//'.txt'

  write(*, '(a, "<", a, ">")') 'filename ', trim(filename)
  open(newunit=ilun, file=trim(filename), form='formatted', status='replace')
  write(ilun, '(a1,a12,a10)') '#', 'Family', 'Count'
  close(ilun)
end program foo

这里's the command I' m使用 rm -f 00001.txt; gfortran foo.f90 -o a.out && ./a.out .

重要提示

使用ifort(在ifort15和ifort18之间尝试的任何版本)以及GNU Fortran(GCC)6.4.1 20171003和GNU Fortran(GCC)7.2.0编译时,这两个代码都运行良好,所以在7.2版本中似乎引入了一个问题.1 of gfortran或与Arch Linux捆绑在一起的版本 .

一些评论

  • 如果在非工作示例中取消注释 nchar = '00001' ,它仍然不起作用 .

  • 如果您将 newunit=ilun 更改为 unit=ilun ,例如 ilun=10 之前,无论如何都适用

系统详细信息

操作系统:GNU Linux发行版:Arch Linux(截至15-12-2017的最新版本)

$ uname -a
Linux manchot 4.14.4-1-ARCH #1 SMP PREEMPT Tue Dec 5 19:10:06 UTC 2017 x86_64 GNU/Linux
$ gfortran --version
GNU Fortran (GCC) 7.2.1 20171128
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

1 回答

  • 2

    此问题与Gfortran 7.2.1的Arch Linux发行版有关 . 它现在已经修复(见https://bugs.archlinux.org/task/56768) .

    如果遇到此问题,则应使用更新安装

    pacman -Syu gcc-fortran
    

相关问题