如何编写包含数组的结构
#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 (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
谢谢