read_file Subroutine

public subroutine read_file(filename, str)

Arguments

Type IntentOptional AttributesName
character(len=*), intent(in) :: filename
character(len=:), intent(out), allocatable:: str

Description

Reads the contents of the file into the allocatable string str. If there are any problems, str will be returned unallocated.

Called By

proc~~read_file~~CalledByGraph proc~read_file read_file proc~test_9 test_9 proc~test_9->proc~read_file program~jf_test_9 jf_test_9 program~jf_test_9->proc~test_9
Help

Variables

TypeVisibility AttributesNameInitial
integer, public :: iunit
integer, public :: istat
integer, public :: filesize

Source Code

    subroutine read_file(filename,str)

    !! Reads the contents of the file into the allocatable string str.
    !! If there are any problems, str will be returned unallocated.
    !!
    !!@warning Will this routine work if the file contains unicode characters??

    implicit none
 
    character(len=*),intent(in) :: filename
    character(len=:),allocatable,intent(out) :: str
 
    integer :: iunit,istat,filesize
 
    open( newunit = iunit,&
          file    = filename,&
          status  = 'OLD',&
          form    = 'UNFORMATTED',&
          access  = 'STREAM',&
          iostat  = istat )
 
    if (istat==0) then
        inquire(file=filename, size=filesize)
        if (filesize>0) then
            allocate( character(len=filesize) :: str )
            read(iunit,pos=1,iostat=istat) str
            if (istat/=0) deallocate(str)
            close(iunit, iostat=istat)
        end if
    end if
 
    end subroutine read_file