Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8113687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:54:34+00:00 2026-06-06T02:54:34+00:00

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

  • 0

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;
    };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:54:35+00:00Added an answer on June 6, 2026 at 2:54 am
    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal: from a list of vectors of equal length, create a matrix where each
My Goal: i want to create an horizontal list of images. when mouse is
I'm trying to create objects from a key/value list. My Problem is, that Object
I have following goal: From a list in main activity that extends ListActivity, I
Goal: to create a percentage column based off the values of calculated columns. Here's
Duplicate: PHP validation/regex for URL My goal is create a PHP regex for website
I am very new to Json and my goal to create the Json output
My goal is to create a canned email on my server and then send
My goal is to create an entry form (addnew.php) that will allow me to
my goal is to create a sort of Javascript library, if you could call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.