【问题标题】:Problem on reading a binary file using fread function使用 fread 函数读取二进制文件的问题
【发布时间】:2019-07-01 18:24:48
【问题描述】:

我的任务是创建一个库存程序,该程序使用结构存储数据并将数据保存到二进制文件中。然后程序应该能够加载数据,即二进制文件中的结构元素。我可以使用 fwrite 函数将输入数据保存到二进制文件中;但是,我在使用 fread 函数将文件加载到程序时遇到问题。

我已经寻找过类似的问题,但我找不到类似的情况,其中 fread 用于将保存的结构元素传递给程序中的结构变量。

这是我的结构的代码

struct details {
    char name[30];
    double price;
    int code;
    int qty;
};

details item[SIZE];

添加物品到库存的功能

int AddItem(int n){
    details inputitem;
    printheader();
    printf("Item Name: ");
    scanf("%s", inputitem.name); //check %[^\n]
    printf("\nItem Price: ");
    scanf("%lf", &inputitem.price);
    printf("\nItem Code: ");
    scanf("%d", &inputitem.code);
    printf("\nQuantity: ");
    scanf("%d", &inputitem.qty);
    printf("\nItem Added! The ID of the item is %d.\n", n+1);
    item[n] = inputitem;
}

显示库存的功能

int DisplayInventory(int n){
    printheader();
    printf("ID |    NAME           |   CODE   |  QUANTITY |  PRICE \n");
    printf("------------------------------------------------------------\n");
    for(int i=0; i<n; i++){
        printf("%d   %-18s   %-10d  %-10d  %-10lf \n",i+1,item[i].name,item[i].code,item[i].qty,item[i].price);
    }
}

保存到二进制文件的功能

int SaveFile(int n){
    FILE *fp;
     fp=fopen("C:\\Users\\Royce\\Documents\\CPEPROG2 Goden Final Project\\Inventory.txt","wb");

      if(!fp) {
             printf("Cannot open file.\n");
             system("pause");
             exit(1);
      }


      for(int i=0; i<n; i++){
          fwrite(&item[i], sizeof(struct details), 1, fp);
      }



      fclose(fp);
      printf("File is saved!\n");

}

加载二进制文件的函数

int LoadFile(int n){
     FILE *fp;
     fp=fopen("C:\\Users\\Royce\\Documents\\CPEPROG2 Goden Final Project\\Inventory.txt","rb");

      if(!fp) {
             printf("Cannot open file.\n");
             system("pause");
             exit(1);
      }


      struct details inputitem;
      int i = 0;
      while(fread(&inputitem, sizeof(struct details),1, fp)){

           item[i] = inputitem; 

           i++;
/*    printf("%d   %-18s   %-10d  %-10d  %-10lf \n",i+1,item[i].name,item[i].code,item[i].qty,item[i].price); */

      }
      fclose(fp);

      printf("File is loaded!\n");  
}

我希望程序在 DisplayInventory 函数中显示二进制文件中保存的结构的详细信息。然而,什么都没有出现。

当我尝试在 LoadFile 函数中打印结构时(使用注释行),所有变量都显示为 0。

编辑 主要功能

int main (){



    int choice; //gets the choice of user from the menu
    bool condition = 1; //loops the menu
    //details item[50]; 
    int count=0; //counts the number of items in the inventory

    do{
        printheader(); //prints the title of the program
        printmenu(); //prints the menu (list of commands)
        scanf("%d", &choice);
        switch(choice){
            case 1: system("cls");
                    AddItem(count);
                    count++; 
                    system("PAUSE"); 
                    system("cls");
                    break;
            case 2: system("cls");
                    //EditItem();
                    system("PAUSE"); 
                    system("cls");
                    break;
            case 3: system("cls");
                    //DeleteItem();
                    system("PAUSE"); 
                    system("cls");
                    break;          
            case 4: system("cls");
                    //ViewItem();
                    system("PAUSE"); 
                    system("cls");
                    break;  
            case 5: system("cls");
                    DisplayInventory(count);
                    system("PAUSE"); 
                    system("cls");
                    break;
            case 6: system("cls");
                    SaveFile(count);
                    system("PAUSE"); 
                    system("cls");
                    break;
            case 7: system("cls");
                    LoadFile(count);
                    system("PAUSE"); 
                    system("cls");
                    break;
            case 8: printf("\nThank you!");
                    exit(0);
                    break;
            default: printf("\nInvalid Input!\n");  
                     getch();
                     system("cls");     

        }
    }while(condition = 1);


    return 0;
}

【问题讨论】:

  • LoadFile() 函数不返回任何值。应该是i的记录数吗?请注意编译器警告,它还应该在此处发布的所有其他函数中暴露缺少的函数返回值。
  • details item[SIZE]; 因为缺少关键字struct,它不会编译,你确定你给出了正确的代码吗?
  • 您使用的是 C 编译器还是 C++ 编译器?
  • 当我把它放在 i++ 行之前时,这些字段的值被打印出来了。但是,我仍然无法在 DisplayInventory 函数中“加载”它们。我不明白。既然 item[] 是全局变量,难道不应该在 load 函数中更新 item[] 结构体数组吗?
  • 关于函数返回值,函数是否应该总是返回一个值,即使它们不是 void?我使用 int 函数是因为我需要将记录数的值从主函数传递给函数。我没有添加返回值,因为我不需要向主函数返回任何值。如果我错了,请纠正我,我在编程方面还不是那么好。谢谢!

标签: c fread


【解决方案1】:

程序从不增加“count”变量,因此当调用“DisplayInventory”时,该值始终为 0,并且不显示任何内容:

返回 'LoadFile()' 中的 i 变量并将其分配给 main() 范围内的 'count' 变量: 在加载文件中:

    ...
    printf("File is loaded!\n");  
    return i;
}

主要:

...
case 7: system("cls");
    count = LoadFile(count);
    system("PAUSE"); 
    system("cls");

注意 'int LoadFile(int n)' 中的 'n' 参数没有被使用,你可以删除它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 2012-02-29
    • 2012-07-17
    相关资源
    最近更新 更多