搞定了。我只是愚蠢而且在一个地方写了=
而不是==
t.t谢谢大家。
我有我的数据文件,现在我想读它并把它放到列表中。我不完全知道怎么做,因为我不得不在不久的将来完成这个项目,我只是请你帮忙;]
头文件:
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);
我的列表函数文件:
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;
}
有什么问题? 程序正在编译好,但是当调用all_recipe_list时它会崩溃。 add_new_element_to_list可能有问题。虽然无法弄明白。 另外我不知道在show_all_list中p-> recipe.category是否正确。
答案 0 :(得分:0)
在add_new_element_to_list()
这一行:
new_el->recipe;
应为:
new_el->recipe = recipe;
我认为。
答案 1 :(得分:0)
尝试将new_el->recipe;
功能中的new_el->recipe = recipe;
行更改为add_new_element_to_list()
。