基本上,我正在尝试开发一个客户订单系统。我编写了一个方法,系统可以获取特定客户的最新订单。要求用户输入客户ID号以获得相应的订单。此功能无法正常工作,因为它收到的是第一个订单,而不是最后一个订单。我在我的方法中使用了 fseek,但它仍然无法正常工作。
void ViewLatestOrder()
{
order o;
char Customerid[10];
ofp=fopen("orders.dat","rb");
printf("\nEnter the Customer ID: \n");
scanf("%s",&Customerid);
rewind(ofp);
fseek(ofp, -sizeof(order), SEEK_END);
while(fread(&o,sizeof(o),1,ofp)==1 && !feof(ofp))
{
if(strcmp(Customerid,o.CustomerID)== 0)
{
printf("\n========================================================\n\n");
printf("\t\t Order Details of %s\n\n",o.CustomerID);
printf("========================================================\n\n");
printf("Product Name: %s\n",o.ProductName);
printf("Product Quantities: %d\n",o.ProductQuantities);
printf("Total Order Price: %.2f\n",o.TotalOrderPrice);
printf("========================================================\n\n");
}
else
{
fseek(ofp, -2*sizeof(order), SEEK_CUR);
}
}
OrdersSubMenu();
fclose(ofp);
}