C Library Functions  - getline (3)

NAME

getline(3f) - [M_io:READ] read a line from specified LUN into allocatable string up to line length limit (LICENSE:PD)

CONTENTS

Syntax
Description
Options
Returns
Example
Author
License

SYNTAX

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

DESCRIPTION

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.

OPTIONS

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.

RETURNS

IER zero unless an error occurred. If not zero, LINE returns the I/O error message.

EXAMPLE

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

AUTHOR

John S. Urban

LICENSE

Public Domain


Nemo Release 3.1 getline (3) July 22, 2023
Generated by manServer 1.08 from 2f3fb8fe-3c1c-4eb9-96c5-0d124c1b15a6 using man macros.