getline(3f) - [M_io:READ] read a line from specified LUN into allocatable string up to line length limit (LICENSE:PD)
Syntax
Description
Options
Returns
Examples
Author
License
function getline(line,lun,iostat) result(ier)
character(len=:),allocatable,intent(out) :: line integer,intent(in),optional :: lun integer,intent(out),optional :: iostat integer :: 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. IOSTAT status returned by READ(IOSTAT=IOS). If not zero, an error occurred or an end-of-file or end-of-record was encountered. This is the same value as returned by the function. See the example program for a usage case.
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,intrinsic :: iso_fortran_env, only : iostat_end use M_io, only : getline implicit none integer :: iostat character(len=:),allocatable :: line open(unit=stdin,pad=yes) INFINITE: do while (getline(line,iostat=iostat)==0) write(*,(a))[//line//] enddo INFINITE if(iostat /= iostat_end)then write(*,*)error reading input:,trim(line) endif end program demo_getline
John S. Urban
Public Domain
Nemo Release 3.1 | getline (3) | February 23, 2025 |