lets say that i have a struct
Code:
typedef struct record{
unsigned char *name;
unsigned int race;
unsigned int classs;
unsigned int level;
unsigned char *guild;
}Record;
and another struct
Code:
typedef struct page{
Record *data[11];
}Page;
given that i've
Code:
fp2 = fopen(argv[2],"wb")) == NULL
newPage = malloc(sizeof(Page))
for(i=0 ; i < 11 ; i++){
if((newPage->data[i] = malloc(sizeof(Record)))==NULL ){
fprintf(stderr,"insufficient memmory");
exit(1);
}
}
for(i=0 ; i < 11 ; i++){
if((newPage->data[i]->name = malloc(sizeof(char)*12))==NULL ){
fprintf(stderr,"insufficient memmory");
exit(1);
}
}
for(i=0 ; i < 11 ; i++){
if((newPage->data[i]->guild = malloc(sizeof(char)*30))==NULL ){
fprintf(stderr,"insufficient memmory");
exit(1);
}
}
}
/*added data till the page is full*/
/*then i fwrite the page struct*/
/*1 record is 45 bytes and there are an array of 11 records so 45*11 = 495*/
fwrite(newPage,495,1,fp2);
how do i fread from the binary after that
i did something along the lines of
Code:
fp = fopen(argv[1],"rb")) == NULL
Page *temp1;
temp1 = malloc(sizeof(Page));
for (i=0; i<11;i++){
temp1->data[i]=malloc(sizeof(Record));
}
for (i=0; i<11;i++){
temp1->data[i]->name=malloc(sizeof(char)*12);
}
for (i=0; i<11;i++){
temp1->data[i]->guild=malloc(sizeof(char)*30);
}
int k;
for(i=0;i<11;i++){
printf("%s",temp1->data[i]->name);
}
and it seg faulted