How do I fwrite a struct containing an array
#include <iostream>
#include <cstdio>
typedef struct {
int ref;
double* ary;
} refAry;
void fillit(double *a,int len){
for (int i=0;i<len;i++) a[i]=i;
}
int main(){
refAry a;
a.ref =10;
a.ary = new double[10];
fillit(a.ary,10);
FILE *of;
if(NULL==(of=fopen("test.bin","w")))
perror("Error opening file");
fwrite(&a,sizeof(refAry),1,of);
fclose(of);
return 0;
}
The filesize of test.bin is 16 bytes, which I guess is (4+8) (int + double*). The filesize should be 4+10*8 (im on 64bit)
~$ cat test.bin |wc -c
16
~$ od -I test.bin
0000000 10 29425680
0000020
~$ od -fD test.bin -j4
0000004 0,000000e+00 7,089709e-38 0,000000e+00
0 29425680 0
0000020
thanks