我想读取文件中的二进制文件,一个struct store,所以当我运行blow代码时,会导致“segment fault”。
FILE *fp2;
Segment *head = (Segment *)malloc(sizeof(Segment));
if ((fp2 = fopen("/home/zhangchengfei/workspace/test/test/1.1428340272.ram", "rb")) == NULL) {
fprintf(stderr,"error to open\n");
}
head->segleter = (Seglet *)malloc(sizeof(Seglet));
head->segleter->objector = (Object *)malloc(sizeof(Object));
int total = (sizeof(Seglet) + sizeof(Object)) + sizeof(Segment);
if (fread(head, total, 1, fp2) == 0) {
}
fclose(fp2);
当我在Segment *head = (Segment *)malloc(sizeof(Segment));
后面fopen()
订购时,就像下面一样,它神奇地过去了。我试了几次,但我没理由?