Read a entire file in one command

Read a entire file in one command

Hi,

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 ! 

Thanks.

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

www.quickthreadprogramming.com

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

  stop

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

if (ierr /= 0) then

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

  stop

  end if

print *,contents(1:400)

call PXFCLOSE (filedes,ier)

end

Steve

Thanks ! Works perfectly !

Login to leave a comment.