Replace the contents of file path (created if needed) with the bytes of text.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | path |
the file to write |
||
| character(len=*), | intent(in) | :: | text |
the new contents |
||
| integer, | intent(out) | :: | stat |
|
||
| character(len=:), | intent(out), | allocatable | :: | errmsg |
the system's message, if |
subroutine write_file(path, text, stat, errmsg) !! Replace the contents of file `path` (created if needed) with the bytes of `text`. character(len=*), intent(in) :: path !! the file to write character(len=*), intent(in) :: text !! the new contents integer, intent(out) :: stat !! `iostat` of the open or write: nonzero on failure character(len=:), allocatable, intent(out) :: errmsg !! the system's message, if `stat` is nonzero integer :: unit character(len=256) :: msg open (newunit=unit, file=path, access='stream', form='unformatted', status='replace', & action='write', iostat=stat, iomsg=msg) if (stat /= 0) then errmsg = trim(msg) return end if write (unit, iostat=stat, iomsg=msg) text if (stat /= 0) errmsg = trim(msg) close (unit) end subroutine write_file