繁体   English   中英

使用fread函数读取二进制文件时出现问题

[英]Problem on reading a binary file using fread function

我的任务是创建一个清单程序,该清单程序使用结构存储数据并将数据保存到二进制文件中。 然后,程序应该能够从二进制文件中加载数据,即结构元素。 我可以使用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;
}

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

返回'LoadFile()'中的i变量,并将其分配给main()范围中的'count'变量:在LoadFile中:

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

在主要方面:

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

请注意,未使用'int LoadFile(int n)'中的'n'参数,您可以将其删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM