read_line Function

public function read_line(iunit, status_ok) result(line)

Reads the next line from a file.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: iunit
logical, intent(out), optional :: status_ok

true if no problems

Return Value character(len=:), allocatable


Called by

proc~~read_line~~CalledByGraph proc~read_line read_line proc~read_file_to_char_array read_file_to_char_array proc~read_file_to_char_array->proc~read_line proc~read_file_to_int_array read_file_to_int_array proc~read_file_to_int_array->proc~read_line proc~read_file_to_int_vec read_file_to_int_vec proc~read_file_to_int_vec->proc~read_line proc~read_line_from_file file_t%read_line_from_file proc~read_line_from_file->proc~read_line

Source Code

    function read_line(iunit,status_ok) result(line)

    integer,intent(in) :: iunit
    character(len=:),allocatable :: line
    logical,intent(out),optional :: status_ok !! true if no problems

    integer :: nread  !! character count specifier for read statement
    integer :: istat  !! file read io status flag
    character(len=chunk_size) :: buffer !! the file read buffer

    nread  = 0
    buffer = ''
    line   = ''
    if (present(status_ok)) status_ok = .true.

    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) .or. 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
        else if (istat==0) then ! all the characters were read
            line = line//buffer  ! add this block of text to the string
        else  ! some kind of error
            if (present(status_ok)) then
                status_ok = .false.
                exit
            else
                error stop 'Read error'
            end if
        end if
    end do

    end function read_line