read_line(3f) - [M_io:READ] read a line from specified LUN into allocatable string up to line length limit cleaning up input line (LICENSE:PD)
Syntax
Description
Options
Returns
Examples
Author
License
function read_line(line,lun,ios) result(ier)
character(len=:),allocatable,intent(out) :: line integer,intent(in),optional :: lun integer,optional :: ios integer :: ier
Read a line of any length up to the 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 but can be set on an open file.
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.
o Append lines that end in a backslash with next line o Expand tabs o Replace unprintable characters with spaces o Remove trailing carriage return characters and white space
LINE the line read from the file. LUN The LUN (logical unit) to read from. Defaults to stdin. IOS 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 status returned by READ(IOSTAT=IER). If not zero, an error occurred or an end-of-file or end-of-record was encountered.
Sample program:Checking the error message and counting lines:
program demo_read_line use,intrinsic :: iso_fortran_env, only : stdin => input_unit use,intrinsic :: iso_fortran_env, only : stderr => error_unit use,intrinsic :: iso_fortran_env, only : iostat_end, iostat_eor use M_io, only : read_line implicit none character (len =: ), allocatable :: line integer :: stat integer :: icount=0 open(unit=stdin,pad=’yes’) INFINITE: do while (read_line(line,ios=stat) == 0) icount=icount write (*, ’(*(g0))’) icount,’ [’,line,’]’ enddo INFINITE if ( .not.is_iostat_end(stat) ) then write (stderr, ’(*(g0))’) & & ’error: line ’,icount,’==>’,trim (line) endif end program demo_read_line
John S. Urban
Public Domain
Nemo Release 3.1 | read_line (3) | February 23, 2025 |