C - 使用printf时的行数限制

时间:2015-01-28 14:08:44

标签: c printf lines

    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");
    }

现在问题是:magazzino.dat中有很多产品,所以当我运行它时,我超出了我可以打印的行的限制,所以它会切断一些产品。我该如何解决这个问题?我知道这里和那里可能会有一些错误,但这不是重点,我仍然需要解决一些问题。我现在唯一的问题是那一个。

我可能有另外一件事是list_transfer函数中的feof(文件)条件。写作是对的: 而(!FEOF(文件)){ 因为我在打印功能结束时得到两个产品,所有字段都设置为零。

非常感谢!

1 个答案:

答案 0 :(得分:0)

如果这确实是一个控制台限制,那么如何执行文件并将其传递给more,即(试图记住DOS术语):

\path\to\program arg1 arg2 | more