我编写了一段代码,用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
这是我正在使用的命令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编译时,这两个代码都运行良好,所以似乎引入了一个问题在gfortran的7.2.1版或与Arch Linux捆绑在一起的版本。
nchar = '00001'
,它仍然无效。newunit=ilun
更改为unit=ilun
,例如ilun=10
之前,无论如何都适用操作系统:GNU Linux 发行版:Arch Linux(截至17-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.
答案 0 :(得分:2)
此问题与Gfortran 7.2.1的Arch Linux发行版有关。它现在已经修复(见https://bugs.archlinux.org/task/56768)。
如果遇到此问题,则应使用
更新安装pacman -Syu gcc-fortran