2
votes

I have a situation unexpected with my fortran program. I want to drop some data in a text file. For some reasons, I created a type to deal with this file.

Therefore, I open a file with the commands (where f is my file type object):

open(newunit    = f%unit,          &
     file       = trim(adjustl(f%name)), &
     form       = 'FORMATTED',      & 
     access     = 'STREAM',         & 
     action     = 'WRITE',          &
     status     = 'REPLACE',        &
     iostat     = ios)
if(ios /= 0) print '("Problem creating file")', trim(f%name)

Then I write stuff like this:

write(unit=f%unit,fmt='(100A)') "#Header"

And when it is done, I want to close the file. To do this, I call the subroutine:

subroutine close_file_ascii(f)    
  implicit none
  class(my_file_type) intent(in) :: f
  logical :: file_opened, file_exist
  integer :: ios

  inquire(unit=f%unit, exist=file_exist, opened=file_opened) !, iostat=ios)
  print *,'file_exist:', file_exist, 'file_opened:', file_opened, 'ios:', ios
  if ((file_opened) .and. (file_exist)) then
     close(unit=f%unit)
  else
    print *,"[WARNING]"
  end if
end subroutine close_file_ascii

My problem is in this last subroutine. When I run the program on windows, I get the following error:

Fortran runtime error: Inquire statement identifies an internal file
Error termination. Backtrace

Therefore, I tried to create MWE to understand the problem, but all of them where working well. So couldn't really isolate the problem. Also a strange thing is that when I compile and execute with gfortran on my linux there is no problem, but when I do so on my windows I get the previous error. ( I compile on windows with gfortran version 7.3.0 x86_64-posix-sjlj-rev0, Built by MinGW-W64 )

I already work-around this problem by uncommenting the end of inquire line in the close subroutine, and everything seems to work fine. And I get the following print:

file_exist: T file_opened: T ios:        5018

But I would to understand what is going on. So what could create such internal file error (while the file should not be internal but external)? Is my workaround could be harmful ? Is it a bug ? Is there a better way to close safely an opened file? Any ideas to isolate the problem ?

EDIT

From roygvib's comment, the following test seems to replicate the problem:

program bug
  implicit none
  integer          :: i
  character(len=1) :: s
  write (s,'(i1)') 0
  open(newUnit=i,file='bug.txt',status='unknown')
  inquire(unit=i)
end program bug
1
What happens if you change the subroutine to take an integer as input and call it as call close_file_ascii(f%unit). Also, where and how is my_file_type defined? - evets
In a small test it works, in the big program I can't try because there is a procedure pointer that can have it as a target. my_file_type is defined before, the first open; with unit and name internal variable. (I know it's painful when there is no MWE...) - R. N
Can't help you if you're unwilling to provide the definition of my_file_type or if it is defined in a module along with the close_file_ascii subprogram. You do realize that ios=5018 signals an error, so no your work-around isn't working fine. - evets
Potentially a bug of gfortran? (if you have used internal file I/O before?) gcc.gnu.org/bugzilla/show_bug.cgi?id=84412 (which says "Known to fail: 7.3.0 8.0") - roygvib
@roygvib, no I have not used internal file I/O before. And when I test the program given in your link I get the same error message. Therefore, if I understand, it is a bug. Right ? - R. N

1 Answers

1
votes

The update to gfortran 8.1 solved the problem.