我有一个结构:
struct Ticket
{
bool IsReserved; // Is the seat resereved (true)
time_t SoldOn; //When has the ticket been bought
double Price; // Price of the ticket
Ticket() { IsReserved = 0; Price = 0; SoldOn = 0; }
};
我已经根据需要在二进制文件中多次保存它,并且我正确读取了它。我的问题是,当用户想买票时,我什么时候才能只修改 1 个结构。例如,当他想购买第 5 个席位时,他应该转到第 5 个结构并仅更改该数据。这是我尝试做的方法。
std::cout<<"\nPlease enter how many tickets would you like to purchase : \n";
do
{
if(number<0 || number > freeSeats)
std::cerr<<"Invalid input.try again: \n";
std::cin>>number;
}while(number<0 || number > freeSeats);
int *t = new int[number];
for(int i = 0; i<number; i++)
{
int seat = 0;
do
{
std::cout<<"Please enter seat "<<i<<" : ";
std::cin>>seat;
file.clear();
file.seekg(0,std::ios::beg);
for(int i =1; i <= seat; i++)
{
serializeTickets(file,ticket);
}
if(ticket.IsReserved == 1)
std::cout<<"The ticket has already been reserved\n";
}while(ticket.IsReserved == 1);
t[i] = seat;
}
file.close();
schedule.serializeTargetLine(choice);
std::ofstream myFile(ticketsFileName,std::ios::binary || std::ios::out);
for(int i = 0; i<number; i++)
{
time_t timer(0);
std::time(&timer);
myFile.seekp(+(t[i] - 1)*sizeof(Ticket),std::ios::beg);
ticket.IsReserved=1;
ticket.Price = atof(schedule.getPrice().c_str());
ticket.SoldOn = timer;
myFile.write(reinterpret_cast<char*> (&ticket),sizeof(Ticket));
}
delete[] t;
myFile.close();