C Library Functions  - fileread (3)

NAME

fileread(3f) - [M_io:READ] read a file into a string array (LICENSE:PD)

CONTENTS

Synopsis
Description
Options
Examples
Author
License

SYNOPSIS

subroutine fileread(filename,pageout)

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

character(len=:),allocatable,intent(out) :: pageout(:)

DESCRIPTION

Read an entire file into memory as a character array, one character variable per line.

NOTE:

Do not 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 filename is 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)

pageout
  array of characters to hold file

EXAMPLES

Sample program

   program demo_fileread
   use M_io,      only : fileread
   implicit none
   character(len=4096)          :: FILENAME   ! file to read
   character(len=:),allocatable :: pageout(:) ! array to hold file in memory
   integer                      :: longest, lines, i
   character(len=*),parameter   :: gen=’(*(g0,1x))’
      ! get a filename
      call get_command_argument(1, FILENAME)
      ! allocate character array and copy file into it
      call fileread(FILENAME,pageout)
      if(.not.allocated(pageout))then
         write(*,gen)’*demo_fileread* failed to load file’,FILENAME
      else
         ! write file from last line to first line
         longest=len(pageout)
         lines=size(pageout)
         write(*,gen)’number of lines is’,lines
         write(*,gen)’and length of lines is’,longest
         write(*,’(a)’)repeat(’%’,longest+2)
         write(*,’("%",a,"%")’)(trim(pageout(i)),i=lines,1,-1)
         write(*,’(a)’)repeat(’%’,longest+2)
         deallocate(pageout)  ! release memory
      endif
   end program demo_fileread

Given

   first line
   second line
   third line

Expected output

   >  number of lines is 3
   >  and length of lines is 11
   > %%%%%%%%%%%%%
   > %third line %
   > %second line%
   > %first line %
   > %%%%%%%%%%%%%

AUTHOR

John S. Urban

LICENSE

Public Domain


Nemo Release 3.1 fileread (3) July 22, 2023
Generated by manServer 1.08 from c16fcca1-d72f-40b8-afb1-5f03b293ff4a using man macros.