0
    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(&current->code, sizeof(current->code), 1, file);
            fread(&current->name, sizeof(current->name), 1, file);
            fread(&current->category, sizeof(current->category), 1, file);
            fread(&current->provenience, sizeof(current->provenience), 1, file);
            fread(&current->quantity, sizeof(current->quantity), 1, file);
            fread(&current->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!

Mazza
  • 3
  • 4
  • 3
    _I exceed the limit on the lines I can print_? what you mean they are more lines that those printable in your console? – Iharob Al Asimi Jan 28 '15 at 14:10
  • yeah I'm talking about the console, sorry (I'm not mother tongue, don't know how to ask these things haha) – Mazza Jan 28 '15 at 14:12
  • 1
    Can't you just scroll the console window up afterwards? – jwodder Jan 28 '15 at 14:12
  • 1
    For `feof()` issues see http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – pmg Jan 28 '15 at 14:14
  • 1
    Which console? cmd? A console of an IDE? – Spikatrix Jan 28 '15 at 14:14
  • yeah, but I'm not able to see the first few products, they just get cut off the console. They told me it's a Windows limitation, but I couldn't find how to fix this – Mazza Jan 28 '15 at 14:14
  • 1
    Oh if it's a windows limitation why don't you try with a diffrent OS? Linux, for example. – Iharob Al Asimi Jan 28 '15 at 14:15
  • 2
    You can increase the console buffer size. If your console is `cmd.exe`, right-click the title bar, and click properties. – Zenadix Jan 28 '15 at 14:17
  • 1
    So it's not a C problem, and your code example is largely irrelevant. As Zenadix implicates above, you can change the buffer size of the console. – Nerf Herder Jan 28 '15 at 14:21
  • I am using CodeBlocks, didn't modify anything, so it should be the stock console... – Mazza Jan 28 '15 at 14:21
  • 1
    Try using system() function to use the command to resize the window as mentioned [here](http://www.stackoverflow.com/questions/945527/modify-cmd-exe-properties-using-the-command-prompt) – Spikatrix Jan 28 '15 at 14:22
  • About the feof question, yes, to be truly safe it should be checked after every fread, to cover partial records. Blank fields are different than null fields though, I'm not exactly sure what you're seeing. – Nerf Herder Jan 28 '15 at 14:26

1 Answers1

0

If this is really a console limitation, how about executing your file and piping it to more, i.e. (trying to remember DOS terminology):

\path\to\program arg1 arg2 | more
abligh
  • 24,573
  • 4
  • 47
  • 84