[UP]


Manual Reference Pages  - read_line (3)

NAME

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

CONTENTS

Syntax
Description
Options
Returns
Example
Author
License

SYNTAX

function read_line(line,lun,ios) result(ier)

   character(len=:),allocatable,intent(out) :: line
   integer,intent(in),optional              :: lun
   integer,optional                         :: ios
   integer                                  :: ier

DESCRIPTION

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.
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
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 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.

RETURNS

IER status returned by READ(IOSTAT=IER). If not zero, an error occurred or an end-of-file or end-of-record was encountered.

EXAMPLE

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

AUTHOR

John S. Urban

LICENSE

Public Domain


read_line (3) March 11, 2021
Generated by manServer 1.08 from 551e0f07-cd87-4a96-afa4-fb3988a92d4a using man macros.