【发布时间】:2020-01-16 17:28:02
【问题描述】:
如何避免在文件处理中重复具有相同产品 ID 的产品,请任何人都可以告诉我它的代码 ..................................................... ..................................................... ..................................................... ......
struct Product
{
int Pid;
char Pname[55];
int balance;
};
void AddProduct()
{
Product p;
cout <<"\nProduct ID: ";
cin>>p.Pid;
cout <<"Product Name: ";
cin>>p.Pname;
cout <<"Balance: ";
cin>>p.balance;
ofstream ofs("Products.bin");
ofs.write(reinterpret_cast<char *>(&p), sizeof(p));
ofs.close();
cout <<"\nProduct Successfully Saved";
}
ProductOpt()
{
char ch;
while(1)
{
cout <<"\n1. Add Product"<<endl;
cout <<"2. Display All Products"<<endl;
cout <<"3. Modify Product"<<endl;
cout <<"4. Delete Product"<<endl;
cout <<"5. Back"<<endl;
ch = getch();
cout<<endl;
if(ch == '1')
AddProduct();
else if(ch == '2')
DisplayProduct();
else if(ch == '3')
ModifyProduct();
else if(ch == '4')
DeleteProduct();
else if(ch == '5')
break;
else
cout <<"Invalid Option"<<endl;
}
}
【问题讨论】:
-
你试过什么?哪里出错了?
-
现在看看.....
标签: c++ file file-handling