1

My goal is to create a list from "menu.bin". This is the func:

    pitem recupera_menu(pitem p){

    pitem novo,aux;

    FILE *f;

                        f=fopen("menu.bin","rb");
                                if(f == NULL)
                                    printf("Erro ao caregar o ficheiro 'menu.bin' \n");

                                novo = (struct item*)malloc(sizeof(item));
                                    if(novo == NULL)
                                        return p;

                                    novo->prox=NULL;


                                    while((fread(novo,sizeof(item),1,f))!=NULL){

                                        if(p==NULL){
                                            p=novo;
                                            aux=p;
                                        }
                                        else{
                                            aux->prox=novo;
                                            aux=aux->prox;
                                        }

                                        printf("%s\n OLE\n",aux->id);

                                    }

                        fclose(f);

                        system("pause");
    return p;
}

this is my struct:

typedef struct item item, *pitem;
struct item{
    char id[5];
    int ing[10];
    float qtd[10];
    pitem prox;
};

For some reason the result of the file isn't the one that should be(it doesn't read the document strait).Maybe someone could help me.

EDIT: well it does run, and prints the "ole" line.The problem is that the file .bin has been completed with the following struct type:

struct item{
    char id[5];
    int ing[10];
    float qtd[10];}

and when i do malloc, i allocate memory to the folowing struct type:

 struct item{
        char id[5];
        int ing[10];
        float qtd[10];
        pitem prox;
    };
DmitryK
  • 1,333
  • 1
  • 11
  • 18
  • I think it might be because of pitem prox; (pointer to the next item of the list) – DmitryK Jun 19 '12 at 23:52
  • 2
    Are you aware of struct alignment? Your structure as defined will have unused pad bytes such that each member is aligned to natural boundaries. You either need to match this in your bin file, or you want to use something like #pragma pack(1) – TJD Jun 20 '12 at 00:05
  • 1
    If the call to this function is made with a non-null p, it will almost certainly crash (aux will be accessed uninitialized). If p is always null upon first call, I am not sure the point of passing it. But beyond this, does the line "OLE" ever print? I am trying to see whether it is failure to read, or failure in making the linked list. Also, upon exit from the loop, aux->prox has to be set to NULL – Virtually Real Jun 20 '12 at 01:51
  • @TJD what you say is true, except if the object is serialized in a binary format, the sizeof operator will reflect the aligned size. – Virtually Real Jun 20 '12 at 01:54
  • edited, hope i made myself clear :) – DmitryK Jun 20 '12 at 08:56

1 Answers1

0
struct item{
        char id[5];
        int ing[10];
        float qtd[10];
    };
struct list{
        struct list *next;
        struct item payload;
    };

Allocate:

struct list *p;
p = malloc (sizeof *p);

read from file:

ret = fread(&p->payload, sizeof p->payload, 1, fp);

Extra: sanitize the loop:

int recupera_menu(struct list **pp){

    int ret,cnt;
    FILE *fp;

    fp = fopen("menu.bin","rb");
    if (!fp) {
        fprintf(stderr, "Erro ao caregar o ficheiro 'menu.bin' \n");
        return 0;
        }

    for (cnt=0;   ;cnt++) {
        *pp = malloc(sizeof **pp);
        if( !*pp ) break;
        (*pp)->next = NULL;

        ret = fread(&(*pp)->payload, sizeof &(*pp)->payload, 1, fp);
        if (ret < 1) break;
        pp = &(*pp)->next;
    }                               

    free (*pp);
    *pp = NULL;
    fclose(fp);
    return cnt
}
wildplasser
  • 43,142
  • 8
  • 66
  • 109
  • Please note that I changed the signature of the function. It needs a pointer to pointer to struct list as argument and returns the number of entries it read. – wildplasser Jun 20 '12 at 11:41