Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Read a entire file in one command

pat97269
Beginner
814 Views

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.

0 Kudos
4 Replies
jimdempseyatthecove
Honored Contributor III
814 Views
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
0 Kudos
pat97269
Beginner
814 Views
Thanks ! I could do that, but i was hoping that fortran has a way of doing it in a simpler way.
0 Kudos
Steven_L_Intel1
Employee
814 Views
You can do it in Fortran with POSIX extensions. Here's an example: [fortran] 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 [/fortran]
0 Kudos
pat97269
Beginner
814 Views
Thanks ! Works perfectly !
0 Kudos
Reply