read_whole_file Subroutine

public subroutine read_whole_file(filename, string, stat)

Read a whole file into an array of characters

Arguments

Type IntentOptional Attributes Name
character(kind=tfc, len=*), intent(in) :: filename

File to read

character(kind=tfc, len=:), intent(out), allocatable :: string

Array of characters representing the file

integer, intent(out) :: stat

Error status


Source Code

subroutine read_whole_file(filename, string, stat)
   !> File to read
   character(*, tfc), intent(in) :: filename
   !> Array of characters representing the file
   character(:, tfc), allocatable, intent(out) :: string
   !> Error status
   integer, intent(out) :: stat

   integer :: io, length

   open(file=filename, &
      & status="old", &
      & access="stream", & 
      & position="append", &
      & newunit=io, &
      & iostat=stat)
   if (stat == 0) then
      inquire(unit=io, pos=length)
      allocate(character(length-1, tfc) :: string, stat=stat)
   end if
   if (stat == 0) then
      read(io, pos=1, iostat=stat) string(:length-1)
   end if
   if (stat == 0) then
      close(io)
   end if
end subroutine read_whole_file