gpt4 book ai didi

c - 使用fread函数读取二进制文件的问题

转载 作者:行者123 更新时间:2023-11-30 16:16:03 26 4
gpt4 key购买 nike

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

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

在主要部分:

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

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

关于c - 使用fread函数读取二进制文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56840639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com