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);
void print(product *head);
void *insert_tail(product *head, int code, char name[], char category[], char provenience[], int quantity, float price);
int search_code(int code, product *iterator);
product *remove_product(int code, product *head);
void free_heap(product *head);
void save_file(product *head, FILE *file);
int main (int argc, char *argv[]){
product *head;
FILE *file, *file_REFRESH;
int outcome;
file = fopen("magazzino.dat", "rb");
if(file != NULL){
head = list_transfer(file);
print(head);
insert_tail(head, 950, "Latta Pomodori", "FruttaVerdura", "Campania", 70, 0.90);
insert_tail(head, 1011, "Olio Bottiglia 1L", "Alimentari", "Puglia", 50, 4.50);
insert_tail(head, 1150, "Biscotti Busta 1Kg", "Alimentari", "Lombardia", 60, 1.50);
insert_tail(head, 1205, "Detersivo Piatti 0.75L", "Pulizia Casa", "Lombardia", 75, 1.10);
print(head);
head = remove_product(985, head);
head = remove_product(1015, head);
head = remove_product(1150, head);
print(head);
file_REFRESH = fopen("magazzino_aggiornato", "wb");
save_file(head, file_REFRESH);
fclose(file);
fclose(file_REFRESH);
free_heap(head);
}
else{
fprintf(stderr, "Non e' stato trovato il file magazzino.dat!\n");
exit(6);
}
return 0;
}
product *list_transfer(FILE *file){
product *head, *current;
head = malloc(sizeof(product));
if(head == NULL){
fprintf(stderr, "Impossibile allocare memoria!\n");
exit(5);
}
memset(head, 0, sizeof(product));
current = head;
rewind(file);
while(!feof(file)){
fread(¤t->code, sizeof(current->code), 1, file);
fread(¤t->name, sizeof(current->name), 1, file);
fread(¤t->category, sizeof(current->category), 1, file);
fread(¤t->provenience, sizeof(current->provenience), 1, file);
fread(¤t->quantity, sizeof(current->quantity), 1, file);
fread(¤t->price, sizeof(current->price), 1, file);
current->next = malloc(sizeof(product));
if(current->next == NULL){
fprintf(stderr, "Impossibile allocare memoria!\n");
exit(5);
}
memset(current->next, 0, sizeof(product));
current = current->next;
}
return head;
}
void print(product *head){
while(head != NULL){
fprintf(stdout, "Code:\t\t%d\n", head->code);
fprintf(stdout, "Name:\t\t%s\n", head->name);
fprintf(stdout, "Category:\t%s\n", head->category);
fprintf(stdout, "Provenience:\t%s\n", head->provenience);
fprintf(stdout, "Quantity:\t%d\n", head->quantity);
fprintf(stdout, "Price:\t\t%.2f\n\n", head->price);
head = head->next;
}
printf("\n");
}
Now the problem is: there are lots of products in magazzino.dat, so when I run it, I exceed the limit on the lines I can print, so it cuts off some products. How can I fix that? I know there might be some mistakes here and there, but that's not the point, I still have to fix some things. My only problem for now is that one.
Maybe another thing I have questions about is the feof(file) condition inside the list_transfer function. Is it right to write: while(!feof(file)){ because I get two product with all fields set to zero at the end of the print function.
Thanks a lot!