help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to use passed Octave string parameters in C code from an Oct file


From: mpender
Subject: How to use passed Octave string parameters in C code from an Oct file
Date: Sat, 22 Mar 2014 12:41:14 -0700 (PDT)

I am trying to access the content of a charMatrix parameter passed from
Octave to an Oct file and I'm having difficulty because the code causes a
segmentation fault every time I run it.  A review of previous postings to
Nabble suggests that there is a potential solution involving a fortran_vec
method, presumably to translate the C++ string format to an array of chars
that would be acceptable to C, but I dont think that explains why the
filename appears like a hexadecimal value instead of what a person would
expect with an overloaded operator<< in C++.  I decided it is time to ask
this group and check some of my assumptions.

A reduced code sample follows:

Octave code:

octave:1> loadcsvfile('tspQuicken.csv')
panic: Segmentation fault -- stopping myself...
Filename: 0x7fffe1872700
attempting to save variables to 'octave-workspace'...
save to 'octave-workspace' complete
Segmentation fault (core dumped)

loadcsvfile.cc:

#include <octave/oct.h>
#include <stdio.h>
#include <string.h>

DEFUN_DLD (loadcsvfile, args, nargout,  "load CSV records from a file.")
{
  octave_value retval;
  int nargin = args.length();

        if (nargin == 1)
        {
// E.g. "tspQuicken.csv"
                charMatrix filename[20] = args(0).char_matrix_value();

                char linebuffer[80];

                FILE * fp;
                octave_stdout << "Filename: " << filename << "\n";
//              printf("Filename: %s\n", (char)filename);
                fp = fopen ((const char *)filename,"r");
        
                if (fp == NULL) perror ("Error opening file");
                else 
                {
                while (fgets (linebuffer, 80, fp) != NULL)
                                printf("%s\n", linebuffer);
                        fclose (fp);
                }
        }
}




--
View this message in context: 
http://octave.1599824.n4.nabble.com/How-to-use-passed-Octave-string-parameters-in-C-code-from-an-Oct-file-tp4663271.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]