Editing Fortran
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
{{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/oct.h> | #include <octave/oct.h> | ||
#include <octave/ls- | #include <octave/ov.h> | ||
#include <fstream> | |||
#include <ls-oct-ascii.h> | |||
#include <octave/octave.h> | |||
#include <octave/parse.h> | |||
#include <octave/toplev.h> | |||
#include <octave/load-save.h> | |||
#include <octave/oct-map.h> | |||
#include <cstring> | |||
int octave_load (const char* filename, const char* varname, double** data, int* numel) | |||
{ | |||
string_vector argv (1); | |||
load_save_format format = LS_ASCII; | |||
install_types (); | |||
argv(0) = varname; | |||
std::ios::openmode mode = std::ios::in | std::ios::binary; | |||
std::string fname (filename); | |||
std::ifstream file (fname.c_str (), mode); | |||
octave_scalar_map m = do_load (file, fname, format, oct_mach_info::flt_fmt_unknown, | |||
false, false, true, argv, 0, 1, 1).scalar_map_value (); | |||
file.close (); | |||
Array<double> M = m.contents (varname).array_value (); | |||
if (error_state) | |||
return -1; | |||
*data = (double*) malloc (M.numel () * sizeof (double)); | |||
*numel = | *numel = M.numel (); | ||
memcpy (*data, M.fortran_vec (), *numel * sizeof (double)); | |||
return 0; | |||
return 0; | |||
} | } | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
{{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 79: | Line 57: | ||
#endif | #endif | ||
int octave_load (const char* filename, double** data, int* numel); | int octave_load (const char* filename, const char* varname, double** data, int* numel); | ||
#ifdef __cplusplus | #ifdef __cplusplus | ||
Line 88: | Line 66: | ||
</syntaxhighlight>}} | </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="fortran" style="font-size:13px"> | ||
program octave_file_io_example | program octave_file_io_example | ||
use iso_c_binding | use iso_c_binding | ||
implicit none | implicit none | ||
interface | interface | ||
function octave_load (filename, data, numel) bind(c, name="octave_load") | function octave_load (filename, varname, data, numel) bind(c, name="octave_load") | ||
use iso_c_binding | use iso_c_binding | ||
implicit none | implicit none | ||
integer(c_int) :: octave_load | integer(c_int) :: octave_load | ||
character(kind=c_char), intent(in) :: filename(*) | character(kind=c_char), intent(in) :: filename(*) | ||
character(kind=c_char), intent(in) :: varname(*) | |||
type(c_ptr), intent(out) :: data | type(c_ptr), intent(out) :: data | ||
integer(c_int), intent(out) :: numel | integer(c_int), intent(out) :: numel | ||
end function octave_load | end function octave_load | ||
end interface | end interface | ||
integer(c_int) :: res | integer(c_int) :: res | ||
type(c_ptr) :: data | type(c_ptr) :: data | ||
real(c_double), pointer :: fdata(:) | real(c_double), pointer :: fdata(:) | ||
integer(c_int) :: numel | integer(c_int) :: numel | ||
res = octave_load (c_char_' | 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/)) | call c_f_pointer (data, fdata, (/numel/)) | ||
write (*,*) fdata | write (*,*) fdata | ||
end program octave_file_io_example | end program octave_file_io_example | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||