filebyte(3f) - [M_io:READ] read (ie. slurp) a file into a character array (LICENSE:PD)
Synopsis
Description
Options
Examples
Author
License
subroutine filebyte(filename,text,length.lines)
character(len=*),intent(in) :: filename or integer,intent(in) :: filenumbercharacter(len=1),allocatable,intent(out) :: text(:) integer,intent(out),optional :: length integer,intent(out),optional :: lines
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.
filename filename to read into memory or LUN (Fortran Logical Unit Number) If a LUN, file must be opened with form=unformatted,access=streamas in
open(unit=igetunit, file=filename, & & action="read", iomsg=message, & & form="unformatted", access="stream", & & status=old,iostat=ios)An exception is that although stdin cannot currently generally be treated as a stream file file the data will be read from stdin if the filename is -.
text array of characters to hold file length returns length of longest line read(Optional). lines returns number of lines read(Optional).
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 readExpected output:! 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
>This is a sample file >that the sample program >will reverse
John S. Urban
Public Domain
Nemo Release 3.1 | filebyte (3m_io) | October 25, 2024 |