getline(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 getline(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 environment 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_getline use,intrinsic :: iso_fortran_env, only : stdin=>input_unit use M_io, only : getline implicit none character(len=:),allocatable :: line open(unit=stdin,pad=yes) INFINITE: do while (getline(line)==0) write(*,(a))[//line//] enddo INFINITE end program demo_getline
John S. Urban
Public Domain
getline (3) | March 11, 2021 |