public subroutine read_file(filename, str)
Arguments
Type |
Intent | Optional |
Attributes | | Name | |
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.
Warning
Will this routine work if the file contains unicode characters??
Variables
Type | Visibility |
Attributes | | Name | | Initial | |
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