Re: binary io
- To: mathgroup at yoda.ncsa.uiuc.edu
- Subject: Re: binary io
- From: xinwei at jessica.stanford.edu
- Date: Wed, 05 Sep 90 11:50:35 -0700
As for getting binary data out from Mathematica, one workaround
for simple functions (without control structures), is to
apply CForm[], then cut & paste the result into a C program]
to generate the data. Of course, you might have to write a small
set of atomic C glue functions for those functions produced by CForm
which aren't in the standard C libraries. Here's a test example
for producing binary data for a NeXT app (I stripped out the
functions originally generated by CForm, to save space.):
#include <stdio.h>
#include <varargs.h>
#include <strings.h>
#include <math.h>
#import <sys/file.h>
#import <sys/types.h>
#import <sys/stat.h>
#define FLOATSIZE (sizeof(float))
#define NUMPOINTS 20
#define PI 3.14159265359
main()
{
int fd;
fd = setupfile();
if (fd >=0)
{
writepts(fd);
close(fd);
}
}
int setupfile()
{
int numpts;
int fd;
fd = open("/xinwei/mobjunk.top", O_CREAT | O_RDWR, 777 );
if (fd < 0)
{
/* char message[256];
sprintf(message,"Cannot open file.");
NXRunAlertPanel(NULL,message,"OK",NULL,NULL); */
return fd;
}
numpts = NUMPOINTS;
write(fd, &numpts, sizeof(int));
return fd;
}
float Sqrt(float x)
{
float xx;
xx = (float) sqrt(x);
return (xx);
}
float Sign(float x)
{
return ((x < 0) ? -1.0 : 1.0) ;
}
float Power(float x, int n)
{
float xx;
xx = (float) pow((double) x, (double) n);
return (xx);
}
float mob1(float t,float s)
{
return(t);
}
float mob2(float t,float s)
{
return (s);
}
float mob3(float t,float s)
{
return(Power(2.7,- Power(t,2) - Power(s,2)));
}
int writepts(fd)
int fd; /* file descriptor */
{
int i,j;
float t,s,x;
int cc;
for (i= 0; i < NUMPOINTS; i++) {
for (j = 0; j < NUMPOINTS; j++) {
t = (i / (float) NUMPOINTS) * 1.6 - 0.8;
s = (j / (float) NUMPOINTS) * 2 * PI;
/* write point to file */
x = mob1(t,s);
cc = write(fd,&x , FLOATSIZE);
x = mob2(t,s);
cc = write(fd,&x , FLOATSIZE);
x = mob3(t,s);
cc = write(fd,&x , FLOATSIZE);
}
}
}