help-octave
[Top][All Lists]
Advanced

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

Octave binary file format specification


From: Paul Laub
Subject: Octave binary file format specification
Date: Tue, 9 Nov 2004 08:18:29 -0800

Dear all, 

Do there exist simple C language programs or code
snippets illustrating how Octave binary file files are
written and read? 

I've found questions like this one in the mailing list
archives. Typical answers are to look at comments in the
source. I have (ls-oct-binary.cc and load-save.cc for
2.1.57). I've produced a binary files that are read
correctly, but only for data of matrix type. There
remain five bytes in the header I still don't
understand and suspect that they will cause my code to
break when I try to save data of another type. (For
details, see code appended below.) 

Is rummaging through source code, trial and error, and
reverse engineering the only way of figuring this out? 

Alternatively, one could write data in Matlab binary
format. I understand that the Mathworks provides
example C code. However, doing so creates a dependence
on their format and their product. 

Once I figure this out, Where would be a good place to
most my example C code (assuming there is interest)? 

Thanks.

Sincerely, 

Paul Laub 


====== snippet of C code illustrating what I know ====
================ and what I don't know ===============

char charvar;
char *datatype = "matrix";
char *magic = "Octave-1-L";
long longvar;  /* ASSUME four byte long integers */

/* Write the 10 + 1 byte file header where charvar is set
to 0x00 to specify little endian byte ordering
characteristic of Wintel machines. (So what is big endian?
0x01?) ASSUME little endian. */
charvar = 0x00;
fwrite(magic, 10, 1, fp);
fwrite(&charvar, 1, 1, fp);

/* Write the header for the one and only data item
composing this file. */
longvar = strlen(octave_var_name);
fwrite(&longvar, 4, 1, fp);  /* length of var name */
fwrite(octave_var_name, longvar, 1, fp); /* var name itself*/

/* "doc" presumably refers to documentation. No
documentation is associated with our data. Moreover, our
data is not global in scope. */
longvar = 0L;
fwrite(&longvar, 4, 1, fp); /* doc length (4 bytes) and doc (0 bytes) */
charvar = 0x00;
fwrite(&charvar, 1, 1, fp); /* global flag (false) */
charvar = 0xff;
fwrite(&charvar, 1, 1, fp); /* data type is typically 255 */

/* Write information about the data type. Here, the data
type is always "matrix". */
longvar = 6;  /* length of "matrix" */
fwrite(&longvar, 4, 1, fp);
fwrite(datatype, longvar, 1, fp);

/* Write four bytes whose origin I do not understand. This
is where breakage is apt to occur.  The FE FF FF FF values
come from looking at octave-generated binary files in a
hex editor. I have NO IDEA what these four bytes refer to!
*/
charvar = 0xfe;
fwrite(&charvar, 1, 1, fp);
charvar = 0xff;
fwrite(&charvar, 1, 1, fp);
fwrite(&charvar, 1, 1, fp);
fwrite(&charvar, 1, 1, fp);

/* Write number of rows of double precision floats. */
longvar = Ndevs;
fwrite(&longvar, 4, 1, fp);

/* Write number of columns. */
longvar = 1;
fwrite(&longvar, 4, 1, fp);

/* Write another number whose origin I do not understand.
Expect breakage here. */
charvar = 0x07;
fwrite(&charvar, 1, 1, fp);

/* All following bytes will be double precision floats. */



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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