0

Got it working. I'm just dumb and wrote = instead == in one place t.t Thanks all.

I have file with my data, and now i want to read it and put it into list. I don't fully know how to do it and since I have to finish this project in near future I simply ask you for help ;]

header file:

typedef struct
{
    char category[50];
    char name[50];
    char ingredients[50]; 
    char instruction[1000];
}recipe_t;



typedef struct element
{
    struct element *next;
    recipe_t recipe;

} el_list;

void all_recipe_list();
void show_all_list(el_list *list);
void add_new_element_to_list(el_list *list, recipe_t formula);

my list functions file:

void all_recipe_list() //reading all record into list + show it(show_all_list function)
{
FILE *database;
recipe_t formula;
el_list *head;

        head = NULL; 

    database = fopen(filename, "rb");
        fgetc(database);        // function feof returns value only if we read something before, so in order to check if its end, we try to read one char 
                                // when writing data to file, I put \n always before new record 
    while (!feof(database))
    {
        fread(&formula, sizeof(recipe_t),1,database);   

        if (head == NULL)
        {   
            head = malloc(sizeof(el_list));
            head->recipe = formula;
            head->next = NULL;
        }
        else
        {
        add_new_element_to_list(head,formula);
        }

        fgetc(database);    // same as above
    }
    fclose(database);


        show_all_list(head);

}


void show_all_list(el_list *list)
{
el_list *p=list;

    while (p != NULL)
    {
        printf("Kategoria:%s\n", p->recipe.category);
        printf("Nazwa:%s\n", p->recipe.name);
        printf("Skaldniki:%s\n", p->recipe.ingredients);
        printf("Instrukcja:%s\n", p->recipe.instruction);

        p = p->next;
    }
}

void add_new_element_to_list(el_list *list, recipe_t formula)
{
el_list *p, *new_el;

        p = list;
        while (p->next != NULL)
        {
            p = p->next;
        }

        new_el = malloc(sizeof(el_list));
        new_el->recipe = formula;
        new_el->next = NULL;
        p->next= new_el;
}

What are the problems? Program is compiling allright but it's crashing when all_recipe_list is called. It's probably something wrong with add_new_element_to_list. Can't figure out what though. Also I don't know if in show_all_list p->recipe.category is right way to do it.

4

2 回答 2

0

add_new_element_to_list()这一行:

new_el->recipe;

应该读:

new_el->recipe = recipe;

我假设。

于 2013-06-23T14:24:38.030 回答
0

尝试将行更改new_el->recipe;new_el->recipe = recipe;函数中的add_new_element_to_list()

于 2013-06-23T14:31:21.873 回答