我有一个名为 AddOrder() 的方法,用户可以在其中创建订单。当系统要求用户输入产品名称时,我想编写一个从 products.dat 文件中获取特定记录的函数。此外,当用户被要求输入产品数量时,我希望系统能够检测到该特定产品的产品数量是否超过了该产品的库存(也存储在 products.dat)中的可用数量。
我尝试了这个函数,但是当我从 AddOrder() 调用它时它不能正常工作。这是我的代码。
void addOrder()
{
order o1;
ofp=fopen("orders.dat","ab");
printf("\n========================================================\n\n");
printf("\t\t Adding an Order\n\n");
printf("========================================================\n\n");
do
{
printf("Enter CustomerID: \n");
scanf("%s",&o1.CustomerID);
}while(!findCustomer(o1.CustomerID));
printf("Enter Product Name: \n");
scanf("%s", o1.ProductName);
int QuantityInStock = getQuantity(o1.ProductName);
printf("%d", &QuantityInStock);
int PQuantity = 0;
printf("Enter Product Quantities: \n");
scanf("%d", &PQuantity);
if(PQuantity > QuantityInStock)
{
printf("You have axeceeded available stock!\n");
}
else
{
printf("Product Quantity is available\n");
}
fwrite(&o1,sizeof(o1),1,ofp);
printf("Order record was added to the system!\n");
fclose(ofp);
}
int getQuantity(const char* ProductName)
{
FILE *pfp;
product p;
int countstock=0;
pfp=fopen("products.dat","rb");
while(1)
{
fread(&p,sizeof(p),1,pfp);
if(feof(pfp))
{
break;
}
if(strcmp(ProductName,p.ProductName)==0)
{
countstock +=&p.QuantityInStock;
}
}
fclose(pfp);
return countstock;
}