Thread: Binaries

  1. #1
    Noob of C
    Join Date
    Apr 2008
    Location
    Melbourne
    Posts
    10

    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
    Last edited by egoveneror2; 04-15-2008 at 12:29 AM. Reason: typo

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Noob of C
    Join Date
    Apr 2008
    Location
    Melbourne
    Posts
    10
    i also did this sry forgot to add this in the previous post

    Code:
    newPage = malloc(sizeof(Page)))
    Last edited by egoveneror2; 04-15-2008 at 01:42 AM. Reason: typo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-03-2009, 04:45 AM
  2. any SDK for build binaries on ARM5 platform?
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 12-14-2006, 11:23 PM
  3. Newbie question Building Library Binaries
    By SkinneyEd in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2005, 10:30 AM
  4. Gift for you all: ASM, Pascal, C and C++ sources and binaries
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 02-03-2003, 08:45 AM
  5. C and C++ binaries
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-10-2002, 12:36 PM