c - 使用 fread 函数读取二进制文件的问题
问题描述
我的任务是创建一个库存程序,该程序使用结构存储数据并将数据保存到二进制文件中。然后程序应该能够加载数据,即二进制文件中的结构元素。我可以使用 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' 参数未使用,您可以将其删除。
推荐阅读
- c# - 如何在 selenium C# 中使用 IWebDriver 检查页面上的复选框?
- java - 使用平方根运算的后缀评估
- python - 访问从另一个模块的功能导入的模块
- java - 在 JAVAFX 的列表视图中更改精确的项目颜色而不更改所有项目颜色
- gcc - 使用硬浮点(PowerPC)时对“__floatundisf”的未定义引用
- python - 生日员工 Django 模型
- bash - Bash:根据键对一列中的值求和
- string-comparison - 计算二元组
- keyboard - 无法使用 Raspberry 4 在 Thonny 中输入方括号
- python - 如果对象的功能不存在,flycheck 如何发出警告?