read_all(3f) - [M_io] read a line from specified LUN into allocatable string up to line length limit (LICENSE:PD)
Syntax
Description
Options
Returns
Example
Author
License
function read_all(line,lun) result(ier)
character(len=:),allocatable,intent(out) :: line integer,intent(in),optional :: lun integer,intent(out) :: ier
Read a line of any length up to programming environments maximum line length. Requires Fortran 2003+.
It is primarily expected to be used when reading input which will then be parsed.
The input file must have a PAD attribute of YES for the function to work properly, which is typically true.
The simple use of a loop that repeatedly re-allocates a character variable in addition to reading the input file one buffer at a time could (depending on the programming environment used) be inefficient, as it could reallocate and allocate memory used for the output string with each buffer read.
LINE line read LUN optional LUN (Fortran logical I/O unit) number. Defaults to stdin.
IER zero unless an error occurred. If not zero, LINE returns the I/O error message.
Sample program:
program demo_read_all use,intrinsic :: iso_fortran_env, only : stdin=>input_unit implicit none open(unit=stdin,pad=yes) ichars=0 msg= do read(stdin,advance=no,size=isize,iostat=ios,fmt=(*(a),t1),iomsg=msg)a if(ios.ne.0) exit ichars=ichars+1 write(*,*)ichars,a enddo write(*,*)END,ichars,isize,trim(msg)end program testit use M_io, only : read_all implicit none character(len=:),allocatable :: line INFINITE: do while (read_all(line)==0) write(*,(a))[//line//] enddo INFINITE end program demo_read_all
John S. Urban
Public Domain
read_all (3) | October 17, 2020 |