C Library Functions  - filebyte (3)

NAME

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

CONTENTS

Synopsis
Description
Options
Examples
Author
License

SYNOPSIS

subroutine filebyte(filename,text,length.lines)

   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 returns length of longest line read(Optional).
lines returns number of lines read(Optional).

EXAMPLES

Sample program, which
  creates test input file "inputfile":
    program demo_filebyte
    use M_io, only      : filebyte
    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,action=’write’) 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 filebyte(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_filebyte

Expected output:

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

AUTHOR

John S. Urban

LICENSE

Public Domain


Nemo Release 3.1 filebyte (3) July 22, 2023
Generated by manServer 1.08 from 5914b2cd-d0ab-48a0-83fc-1ec50d1536e9 using man macros.