Read a entire file in one command

Read a entire file in one command


Is there a way to read an entire file (only text, example kernel for opencl ) to a character(len=10000) in one read command ?  

I can't seem to have more than the first line for formatted file, and i missed the  first column  but that's obviously because it is not in unformatted format ! 


5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

If you are more comfortable with C/C++ you can call a C/C++ function to perform the open, allocate buffer, read and return pointer to buffer and file size.
You can then use the interoperability features to convert the C pointer to FORTRAN pointer. Add a C/C++ function to delete the buffer (if buffer was allocated). I think you are not intending on parsing this file, just reference the buffer when launching the OpenCL portion of your applicaiton.

Jim Dempsey

Thanks ! I could do that, but i was hoping that fortran has a way of doing it in a simpler way.

You can do it in Fortran with POSIX extensions. Here's an example:

use ifposix

character(10000) contents

character(*), parameter :: filename = 'path_to_file'

integer bytes_read, filedes, ierr
call PXFOPEN (filename,len(filename),IPXFCONST('O_RDONLY'),0,filedes,ierr)

if (ierr /= 0) then

  print *, "Open failed with POSIX error", ierr


  end if
call PXFREAD (filedes,contents,len(contents),bytes_read,ierr)

if (ierr /= 0) then

  print *, "Read failed with POSIX error", ierr


  end if

print *,contents(1:400)

call PXFCLOSE (filedes,ier)


Retired 12/31/2016

Thanks ! Works perfectly !

Leave a Comment

Please sign in to add a comment. Not a member? Join today