All bytes of file path.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | path |
the file to read |
||
| character(len=:), | intent(out), | allocatable | :: | text |
the contents; |
|
| integer, | intent(out) | :: | stat |
|
||
| character(len=:), | intent(out), | allocatable | :: | errmsg |
the system's message, if |
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