read_line_from_file Subroutine

private subroutine read_line_from_file(iunit, line, eof)

Read a single line from a file.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: iunit

the file unit (assumed to be opened)

character(len=:), intent(out), allocatable :: line

the line read

logical, intent(out) :: eof

if end of file reached


Called by

proc~~read_line_from_file~~CalledByGraph proc~read_line_from_file namelist_parser_module::read_line_from_file proc~parse_namelist namelist_parser_module::parse_namelist proc~parse_namelist->proc~read_line_from_file

Source Code

    subroutine read_line_from_file(iunit,line,eof)

    implicit none

    integer,intent(in) :: iunit  !! the file unit (assumed to be opened)
    character(len=:),allocatable,intent(out) :: line !! the line read
    logical,intent(out) :: eof   !! if end of file reached

    integer,parameter :: buffer_size = 256  !! the size of the read buffer [arbitrary]

    integer :: nread  !! character count specifier for read statement
    integer :: istat  !! file read `iostat` flag
    character(len=buffer_size) :: buffer !! the file read buffer

    nread  = 0
    buffer = ''
    line   = ''
    eof    = .false.

    do
        ! read in the next block of text from the line:
        read(iunit,fmt='(A)',advance='NO',size=nread,iostat=istat) buffer
        if (IS_IOSTAT_END(istat)) then
            ! add the last block of text before the end of the file
            if (nread>0) line = line//buffer(1:nread)
            eof = .true.
            exit
        elseif (IS_IOSTAT_EOR(istat)) then
            ! add the last block of text before the end of record
            if (nread>0) line = line//buffer(1:nread)
            exit
        elseif (istat==0) then ! all the characters were read
            line = line//buffer ! add this block of text to the string
        else  ! some kind of error
            write(*,*) 'istat=',istat
            error stop 'Read error.'
        end if
    end do

    end subroutine read_line_from_file