write_file Subroutine

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

Replace the contents of file path (created if needed) with the bytes of text.

Arguments

Type IntentOptional Attributes Name
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=:), intent(out), allocatable :: errmsg

the system's message, if stat is nonzero


Called by

proc~~write_file~~CalledByGraph proc~write_file write_file proc~process_text process_text proc~process_text->proc~write_file proc~process_file process_file proc~process_file->proc~process_text proc~process_stdin process_stdin proc~process_stdin->proc~process_text proc~run run proc~run->proc~process_file proc~run->proc~process_stdin

Source Code

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