配列を含む構造体を fwrite する方法
#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;
}
test.bin のファイルサイズは 16 バイトで、おそらく (4+8) (int + double*) です。ファイルサイズは 4+10*8 にする必要があります (64 ビットの場合)
~$ 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
ありがとう