[UP]


Manual Reference Pages  - slurp (3)

NAME

SLURP(3f) - [M_io] read a file into a character array (LICENSE:PD)

CONTENTS

Synopsis
Description
Options
Examples
Author
License

SYNOPSIS

subroutine slurp(filename,text)

   character(len=*),intent(in) :: filename
    or
   integer,intent(in)          :: filenumber

character(len=1),allocatable,intent(out) :: text(:) integer,intent(out),optional :: length integer,intent(out),optional :: lines

DESCRIPTION

Read an entire file as a stream into memory as an array of single characters, retaining line end terminators.

NOTE:

Never casually read an entire file into memory if you can process it per line or in smaller units; as large files can consume unreasonable amounts of memory.

OPTIONS

filename
  filename to read into memory or LUN (Fortran Logical Unit Number) If a LUN, file must be opened with
                    form=’unformatted’,access=’stream’

  as in

                   open(unit=igetunit, file=filename,     &
                   & action="read", iomsg=message,        &
                   & form="unformatted", access="stream", &
                   & status=’old’,iostat=ios)

text array of characters to hold file
length length of longest line read(Optional).
lines number of lines read(Optional).

EXAMPLES

Sample program, which
  creates test input file "inputfile":
    program demo_slurp
    use M_io, only      : slurp
    implicit none
    character(len=1),allocatable :: text(:) ! array to hold file in memory
    character(len=*),parameter :: FILENAME=’inputfile’ ! file to read

! create test file open(file=FILENAME,unit=10) write(10,’(a)’) new_line(’A’)//’esrever lliw’ write(10,’(a)’) ’margorp elpmas eht taht’ write(10,’(a)’) ’elif elpmas a si sihT’ close(unit=10)

call slurp(FILENAME,text) ! allocate character array and copy file into it

if(.not.allocated(text))then write(*,*)’*rever* failed to load file ’//FILENAME else ! write file reversed to stdout write(*,’(*(a:))’,advance=’no’)text(size(text):1:-1) deallocate(text) ! release memory endif

end program demo_slurp

Expected output:

    >This is a sample file
    >that the sample program
    >will reverse

AUTHOR

John S. Urban

LICENSE

Public Domain


slurp (3) March 11, 2021
Generated by manServer 1.08 from b15dfb88-df0d-4ea2-969e-783be20a0c86 using man macros.