C++ Binary File Modifying
-
Hi guys I was trying to write some data in binary file and then display it. Everything works good but I tried to add one more options which is modify the data by using item id but I couldn't finish it can you guys show my mistake in my code thank you fstream binaryFile("Inventory.dat", ios::in | ios::out | ios::binary); Inventory obj; int userInput; cin >> userInput; while (binaryFile.read((char*)&obj, sizeof(obj))) { if (userInput = *obj.itemId) { cout << "Enter item description :: " << flush; cin.getline(obj.description, DESCRIPTION_LENGTH); // Character Array Getline while (!isValidInput(obj.quantity, 0, INT_MAX, "Enter quantity on hand :: >> ", "Error: number must be greater than 0")); while (!isValidInput(obj.wholeSalePrice, 0.0, DBL_MAX, "Enter whole sale price :: >> ", "Error: number must be greater than 0")); while (!isValidInput(obj.retailPrice, 0.0, DBL_MAX, "Enter retail price :: >> ", "Error: number must be greater than 0")); cin.ignore(INT_MAX, '\n'); cout << "Enter date added (MM/DD/YYYY) :: " << flush; cin.getline(obj.dataAdded, DATE_LENGTH); cout << "Enter item id (max 5 digit ) :: " << flush; cin.getline(obj.itemId, ITEM_ID_LENGTH); int pos = -1 * sizeof(obj); binaryFile.seekp(pos, ios::cur); binaryFile.write((char*)&obj, sizeof(obj)); } } binaryFile.close(); }