# Binaries

• 04-15-2008
egoveneror2
Binaries
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("&#37;s",temp1->data[i]->name);   }```
and it seg faulted
• 04-15-2008
vart
fwrite(newPage,495,1,fp2);
your memory is not continues - so you cannot do it

your newPage pointer points to 44 bytes of continues memory (11 pointers) - it is all you can write with 1 fwrite operation

for each malloced array - you need another fwrite

if you want to write all the Page with childs - make the childs static arrays
• 04-15-2008
egoveneror2
i also did this sry forgot to add this in the previous post

Code:

`newPage = malloc(sizeof(Page)))`