help-octave
[Top][All Lists]
Advanced

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

Re: Read/Write binary files using C++ bindings


From: David Grundberg
Subject: Re: Read/Write binary files using C++ bindings
Date: Mon, 14 Jun 2010 10:01:45 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100317 Thunderbird/3.0.4

On 06/14/2010 06:33 AM, Chidambaram Annamalai wrote:
On 6/9/10, Chidambaram Annamalai<address@hidden>  wrote:
I have the following piece of code to write and read a matrix to and
from a file in binary. The writing works fine but the reading code
gives a Segmentation Fault error.

According to the C++ API doc:

OCTINTERP_API std::string read_binary_data      (       std::istream&       is,
                bool    swap,
                oct_mach_info::float_format     fmt,
                const std::string&          filename,
                bool&       global,
                octave_value&       tc,
                std::string&        doc     
        )                       

and I have provided the read_binary_data function with arguments
accordingly (closely following the implementation of do_load(..) from
here [1])

#include<iostream>
#include<fstream>

// BEGIN CODE

// octave headers
#include<octave/oct.h>
#include<octave/octave.h>

#include<octave/dMatrix.h>        /* Real double precision matrices */
#include<octave/oct-rand.h>       /* Random number generator        */
#include<octave/load-save.h>      /* Load/Save                      */
#include<octave/ls-oct-binary.h>  /* Binary format                  */

int
main(int argc, char **argv)
{
     Matrix A(2,2);
     A(0,0) = 1.0; A(0,1) = 2.0;
     A(1,0) = 3.0; A(1,1) = 4.0;

     std::cout<<  A<<  std::endl;

     std::ofstream os;
     bool global(false), writefloat(true);

     os.open("A.bin");
     write_header (os, LS_BINARY);
     save_binary_data (os, octave_value (octave_value (A)),
                      std::string("A"), std::string(""),
                      global, writefloat);
     os.close();

     std::ifstream is;
     bool swap;
     std::string name, doc;
     octave_value B;
     oct_mach_info::float_format flt_fmt;

     is.open("A.bin");
     read_binary_file_header (is, swap, flt_fmt);
     read_binary_data (is, swap, flt_fmt, name, global, tc, doc);
     is.close();

     return 0;
}

// END OF CODE

For convenience here's the Makefile that I used to compile it:

all: readwrite
readwrite: readwrite.o
         mkoctfile --link-stand-alone -o readwrite readwrite.o
readwrite.o: readwrite.cpp
         g++ -c -I/usr/include/octave-3.0.1/octave/
-I/usr/local/cuda/include/ -o readwrite.o readwrite.cpp

Help? :)

Best,
Chillu

[1]:
http://octave3.0.sourcearchive.com/documentation/3.0.5/load-save_8cc-source.html


Bump.

Can someone please help me with this? I'm trying to perform a simple
write/read of a matrix using Octave's C++ bindings. I posted this
query a week back but I still got no replies.

Thanks,
Chillu


I would just push a new scope, read the file using the builtin "load" (called through feval) and just pick data from the scope variables. Much more stable API than trying to mimic the builtins.

David


reply via email to

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