Bug 95476 - Reading file of length zero
Summary: Reading file of length zero
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-06-02 08:43 UTC by Martin Diehl
Modified: 2020-06-07 13:29 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Diehl 2020-06-02 08:43:25 UTC
There is some ambiguity when reading a file of zero length:

program test

 character(len=:), allocatable :: a
 a = IO_read('empty.txt')

 print*, len(a)
 print*, '#'//a//'#'

contains

function IO_read(fileName) result(fileContent)

  character(len=*),  intent(in) :: fileName
  character(len=:), allocatable :: fileContent
  integer ::  &
    fileLength, &
    fileUnit, &
    myStat

  inquire(file = fileName, size=fileLength)
  open(newunit=fileUnit, file=fileName, access='stream',&
       status='old', position='rewind', action='read',iostat=myStat)
  if(myStat /= 0) stop 1
  allocate(character(len=fileLength)::fileContent)
  read(fileUnit,iostat=myStat) fileContent
  print*, mystat
  close(fileUnit)

end function IO_read

end program test

see also the discussion on 
https://software.intel.com/en-us/comment/1960049#comment-1960049
Comment 1 Thomas Koenig 2020-06-07 08:29:07 UTC
What is the expected behavior?
Comment 2 Martin Diehl 2020-06-07 13:29:42 UTC
(In reply to Thomas Koenig from comment #1)
> What is the expected behavior?

According to the comment by S. Lionel, the behavior of gfortran conforms to the standard