Reads the next line from a file.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | iunit | |||
| logical, | intent(out), | optional | :: | status_ok |
true if no problems |
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