Fortran: Difference between revisions

1,101 bytes added ,  29 November 2012
no edit summary
No edit summary
No edit summary
Line 1: Line 1:




{{Code|C++ function to load a matrix from an ASCII file in Octave native format|<syntaxhighlight lang="C" style="font-size:13px">
{{Code|octave_file_io.cc: C++ function to load a matrix from an ASCII file in Octave native format|<syntaxhighlight lang="C" style="font-size:13px">
#include <octave_file_io.h>
#include <octave_file_io.h>


Line 49: Line 49:




{{Code|C interface to a function linking to liboctave|<syntaxhighlight lang="C" style="font-size:13px">
{{Code|octave_file_io.h: header file with C interface to octave_file_io.cc|<syntaxhighlight lang="C" style="font-size:13px">
#ifndef OCTAVE_FILE_IO_H
#ifndef OCTAVE_FILE_IO_H
#define OCTAVE_FILE_IO_H
#define OCTAVE_FILE_IO_H
Line 64: Line 64:


#endif
#endif
</syntaxhighlight>}}
Compile the function
  mkoctfile -I. octave_file_io.cc
{{Code|octave_file_io.cc: C++ function to load a matrix from an ASCII file in Octave native format|<syntaxhighlight lang="C"
program octave_file_io_example
 
  use iso_c_binding
   
  implicit none
  interface
    function octave_load (filename, varname, data, numel) bind(c, name="octave_load")
     
      use iso_c_binding
      implicit none
      integer(c_int) :: octave_load
     
      character(kind=c_char), intent(in) :: filename(*)
      character(kind=c_char), intent(in) :: varname(*)
     
      type(c_ptr), intent(out) :: data
      integer(c_int), intent(out) :: numel
     
    end function octave_load
  end interface
 
  integer(c_int) :: res
  type(c_ptr) :: data
  real(c_double), pointer :: fdata(:)
  integer(c_int) :: numel
  res = octave_load (c_char_'pippo.octxt' // c_null_char, c_char_'A' // c_null_char, data, numel)
 
  call c_f_pointer (data, fdata, (/numel/))
 
  write (*,*) fdata
end program octave_file_io_example
</syntaxhighlight>}}
</syntaxhighlight>}}
349

edits