read_file Subroutine

public subroutine read_file(path, text, stat, errmsg)

All bytes of file path.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: path

the file to read

character(len=:), intent(out), allocatable :: text

the contents; '' if the file cannot be opened

integer, intent(out) :: stat

iostat of the open or read: nonzero on failure

character(len=:), intent(out), allocatable :: errmsg

the system's message, if stat is nonzero


Called by

proc~~read_file~~CalledByGraph proc~read_file read_file proc~process_file process_file proc~process_file->proc~read_file proc~run run proc~run->proc~process_file

Source Code

subroutine read_file(path, text, stat, errmsg)
    !! All bytes of file `path`.
    character(len=*), intent(in) :: path   !! the file to read
    character(len=:), allocatable, intent(out) :: text      !! the contents; `''` if the file cannot be opened
    integer, intent(out) :: stat                            !! `iostat` of the open or read: nonzero on failure
    character(len=:), allocatable, intent(out) :: errmsg    !! the system's message, if `stat` is nonzero
    integer :: unit, nbytes
    character(len=256) :: msg

    text = ''
    open (newunit=unit, file=path, access='stream', form='unformatted', status='old', &
          action='read', iostat=stat, iomsg=msg)
    if (stat /= 0) then
        errmsg = trim(msg)
        return
    end if
    inquire (unit=unit, size=nbytes)
    if (nbytes > 0) then
        deallocate (text)
        allocate (character(len=nbytes) :: text)
        read (unit, iostat=stat, iomsg=msg) text
        if (stat /= 0) errmsg = trim(msg)
    end if
    close (unit)
end subroutine read_file