いくつかのバイナリ ファイルを読み取って、動的リストに保存する必要があります。以下を参照して、私が行った方法でそれを実行し、読み取ったすべての製品を印刷すると、コンソールには、すべてのフィールドが 0 に設定された (または配列の場合は欠落している) 2 つの製品が表示されます。何が間違っているのか理解できません。修正を手伝ってもらえますか?
typedef struct product_temp{
int code;
char name[MAX_NAME];
char category[MAX_CATEGORY];
char provenience[MAX_PROVENIENCE];
int quantity;
float price;
struct product_temp *next;
} product;
product *list_transfer(FILE *file){
product *head, *current, *current_temp;
int end = 0, i = 0, outcome;
while(end == 0){
if(i == 0){
current = malloc(sizeof(product));
if(current == NULL){
fprintf(stderr, "Impossibile allocare memoria!\n");
exit(6);
}
memset(current, 0, sizeof(product));
head = current;
}
else{
current->next = malloc(sizeof(product));
if(current->next == NULL){
fprintf(stderr, "Impossibile allocare memoria!");
exit(6);
}
memset(current->next, 0, sizeof(product));
current = current->next;
memset(current, 0, sizeof(product));
}
if((outcome = fread(¤t->code, sizeof(current->code), 1, file)) == 0){
end = 1;
}
if((outcome = fread(¤t->name, sizeof(current->name), 1, file)) == 0){
end = 1;
}
if((outcome = fread(¤t->category, sizeof(current->category), 1, file)) == 0){
end = 1;
}
if((outcome = fread(¤t->provenience, sizeof(current->provenience), 1, file)) == 0){
end = 1;
}
if((outcome = fread(¤t->quantity, sizeof(current->quantity), 1, file)) == 0){
end = 1;
}
if((outcome = fread(¤t->price, sizeof(current->price), 1, file)) == 0){
end = 1;
}
if(feof(file)){
end = 1;
free(current);
}
i++;
}
return head;
}