我使用c ++进行编码,并且遇到了程序特定功能的问题。我写到文件的末尾,但每次我尝试将记录添加到文件时,它都会覆盖以前的记录。经过测试,我知道我所包含的具体代码是带有问题的代码。谁能告诉我我做错了什么?我到处寻找,无法找到答案。
另外,我知道该程序现在非常非结构化。这是待办事项列表中的下一个。
if(userI == 1){
infoFile.open("inventory.dat", ios::out | ios::binary);
infoFile.seekg(0, ios::end);
cout << "Thank you for choosing to enter a new record." << endl;
cout << "Please enter a description of the item up to 50 characters: ";
getline(cin,description);
strcpy(records.description, description.c_str());
cin.clear();
cin.sync();
cout << "Please enter the quantity on hand: ";
cin >> records.quantity;
cin.clear();
cin.sync();
cout << "Please enter the wholesale cost of the item: ";
cin >> records.wholesale;
cin.clear();
cin.sync();
cout << "Please enter the retail cost of the item: ";
cin >> records.retail;
cin.clear();
cin.sync();
cout << "Please enter the date in the form dd/mm/yyyy: ";
cin >> records.date;
cin.clear();
cin.sync();
infoFile.write(reinterpret_cast<char*> (&infoFile), sizeof(infoFile));
infoFile.close();
}