2015-01-28 3 views
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"); 
    } 

이제 문제는 magazzino.dat에 많은 제품이 있기 때문에 실행할 때 인쇄 할 수있는 행의 한계를 초과하여 일부를 잘라냅니다. 제작품. 어떻게 해결할 수 있습니까? 나는 여기 저기에 약간의 실수가 있을지도 모른다는 것을 안다. 그러나 그것은 요점이 아니다, 나는 여전히 몇 가지를 고쳐야 만한다. 내 유일한 문제는 바로 그것입니다.C - printf를 사용할 때 한도 제한

아마도 또 다른 질문은 list_transfer 함수 내부의 feof (파일) 조건입니다. 인쇄 기능의 끝에서 모든 필드가 0으로 설정되어있는 두 제품을 얻었 기 때문에 (! feof (file)) { while 작성하는 것이 옳은가요?

고맙습니다.

+3

_i 내가 print_ 수있는 선에서 제한을 초과? 당신은 그들이 콘솔에 인쇄 할 수있는 라인이 더 많다는 것을 의미합니까? –

+0

예 콘솔에 대해 이야기하고 있습니다. 죄송합니다. (모국어가 아니며, 이런 것들을 묻는 법을 모릅니다.) – Mazza

+1

콘솔 창을 스크롤 할 수 없습니까? – jwodder

답변

0

이 경우 정말 콘솔 제한하는 방법 즉, (DOS 용어를 기억하려고), 파일을 실행하고 more에 배관에 대한 :

\path\to\program arg1 arg2 | more