Here's a simple function that's supposed to write to a file (random access file), but it doesn't seem to work. Any ideas why?
Thanks!!
Code:
void addRecord(fstream& file)
{
InventoryItem invItem;
// InventoryItem tempInvItem;
char tempSerialNum[3];
int tempQuantity;
double tempPrice;
// double tempProfitLoss;
long recNum;
cout << "Enter data for the new record: " << endl
<< "Serial Number: " << endl;
cin >> tempSerialNum;
recNum = atol(tempSerialNum);
if(!validRecNum(recNum))
return;
invItem.putSerialNumber(tempSerialNum);
cout << "Quantity: " << endl;
cin >> tempQuantity;
invItem.putQuantity(tempQuantity);
cout << "Price: " << endl;
cin >> tempPrice;
invItem.puPrice(tempPrice);
// file.seekp((recNum - 1) * sizeof(InventoryItem), ios::beg);
// file.read((char *)&tempInvItem, sizeof(InventoryItem));
//if (strcmp(tempInvItem.serialNumber, "") == 0)
// {
file.seekp((recNum - 1) * sizeof(InventoryItem), ios::beg);
file.write((char *)&invItem,sizeof(InventoryItem));
// file.flush();
cout << "Record " << invItem.getSerialNumber() << " added to file.";
// }
}