grep "struct test"* 可以查看之前“”里面的东西再那个文件
struct test{
int point;
char data;
};
int main(){
struct test a={100,'p'};
struct test b;
FILE*fd;
fd=fopen("./file1","w+");
int n_write=fwrite(&a,sizeof(struct test),1,fd);
fseek(fd,0,SEEK_SET);
int n_read=fread(&b,sizeof(struct test),1,fd);
printf("read b=%d,%c\n",b.point,b.data);
// printf("read b[1]=%d,%c\n",b[1].point,b[1].data)
fclose(fd);
return 0;
}
那如果写结构体数组呢?(指针)
struct test{
int point;
char data;
};
int main(){
struct test a[2]={{100,'p'},{200,'q'}};
struct test b[2];
FILE*fd;
fd=fopen("./file1","w+");
int n_write=fwrite(a,sizeof(struct test),2,fd);
fseek(fd,0,SEEK_SET);
int n_read=fread(b,sizeof(struct test),2,fd);
printf("read b=%d,%c\n",b[1].point,b[1].data);
// printf("read b[1]=%d,%c\n",b[1].point,b[1].data)
fclose(fd);
return 0;
}