2016-11-25 61 views
-1

好的,我得到了一個文本文件,它有一堆的id,大小和權重。抓取這個數據的函數在底部void listBoxes(const char filename [])。如果用戶輸入1,它應該列出文本文件中的所有信息以及我輸入的一些標題。當我在我的main,case 1中調用函數時,它僅打印標題,並在嘗試打印實際數據時發生錯誤。我知道我只是部分調用了正確的函數,但我不知道如何從函數中獲取數據。調用函數

// Workshop 9 - Files 
// Name: 
// Student #: 

#include <stdio.h> 

struct Box 
{ 
    int id;  // the box ID 
    double size[3]; // dimensions of the box (Length, Width, Height) 
    double weight; // weight of the box 
}; 

void printBox(struct Box b) 
{ 
    printf("\nID:  %6d\n" 
      "Length: %6.2lf\n" 
      "Width: %6.2lf\n" 
      "Height: %6.2lf\n" 
      "Weight: %6.2lf\n\n", b.id, b.size[0], b.size[1], b.size[2], b.weight); 
} 

int menu(void) 
{ 
    int choice = -1; 

    printf("1- List all boxes\n"); 
    printf("2- Find a box\n"); 
    printf("3- Add a box\n"); 
    printf("4- Randomly pick a lucky box!\n"); 
    printf("0- Exit program\n"); 

    printf("Select an option: "); 
    do 
    { 
     scanf("%d", &choice); 
     if (choice < 0 || choice > 4) 
      printf("Please enter a number between 0 and 4: "); 
    } while (choice < 0 || choice > 4); 
    return choice; 
} 

int main(void) 
{ 
    struct Box b; 

    FILE * fp = NULL; 

    int choice; // , boxID, r; 

    char filename[] = "storage.txt"; 

    printf("Welcome to My Storage Room\n"); 
    printf("==========================\n"); 
    do { 
     // get user's choice 
     choice = menu(); 

     switch (choice) { 
      case 1: 
       // IN_LAB: list items 
       listBoxes(filename); 
       break; 

      case 2: 
       // IN_LAB: find a box given its ID 
       // ask for ID 

       // call displayBox 

       break; 

      case 3: 
       // AT_HOME: add a box 
       // get user input for box's ID, size and weight 

       // call addBox, print message to show number of boxes added 

       break; 

      case 4: 
       // AT_HOME: randomly pick a lucky box 

       // choose a random number between 1 and the number of boxes in storage 
       // display the lucky box! 

       break; 
     }; 

    } while (choice > 0); 

    return 0; 
} 

void listBoxes(const char filename[]) 
{ 
    struct Box b; 

    FILE * fp = NULL; 

    fp = fopen("storage.txt", "r"); 

    if (fp != NULL) 
    { 
     printf("List of boxes\n"); 
     printf("=============\n"); 
     printf("ID Length Width Height Weight\n"); 
     printf("-----------------------------\n"); 
     fscanf("%2d %6.2lf %5.2lf %6.2lf %6.2lf\n", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.size[3], &b.weight); 
     fclose(fp); 
    } 
    else 
    { 
     printf("Error opening file\n"); 
    } 
    return 0; 
} 

回答

1

與您listBoxes()功能的問題是由於不正確的使用fscanf()功能。由手冊給出的fscanf()的正確用法:

int fscanf(FILE *stream, const char *format, ...);

您還沒有指定從中讀取輸入的文件指針。另外,您只給出了格式化打印功能使用的格式信息,並且引用了不存在的b.size[]的額外索引。正確的用法是:

fscanf(fp, "%d %lf %lf %lf %lf\n", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight); 

您還沒有實際打印讀入的信息,因此您應該按照您希望的格式進行此操作。

最後,您不需要returnvoid函數。

+0

謝謝。我的教科書說fscanf從文件中讀取,所以我錯誤地認爲閱讀與印刷承擔相同的責任。 –