// This program sets up a file of blank inventory records. #include #include using namespace std; const int DESC_SIZE = 31, NUM_RECORDS = 5; // Declaration of Invtry structure. struct Invtry { char desc[DESC_SIZE]; int qty; double price; }; int main() { // Variables needed to read and write the file. Invtry record = { "", 0, 0.0 }; long recNum; // Create file object and open file. fstream inventory("invtry.dat", ios::in | ios::out | ios::binary); if (!inventory) { cout << "Error opening file."; return 0; } // Now write the blank records. for (int count = 0; count < NUM_RECORDS; count++) { cout << "Now writing record " << count << endl; inventory.write(reinterpret_cast(&record), sizeof(record)); // inventory.write((char *)&record, sizeof(record)); //older method of type casting } // Rewind the file inventory.seekg(0L, ios::beg); // Now read and display the records cout << endl << "Now reading and displaying the records" << endl << endl; inventory.read(reinterpret_cast(&record), sizeof(record)); // inventory.read((char *)&record, sizeof(record)); //older method of type casting while (!inventory.eof()) { cout << "Description: "; cout << record.desc << endl; cout << "Quantity: "; cout << record.qty << endl; cout << "Price: "; cout << record.price << endl << endl; inventory.read(reinterpret_cast(&record), sizeof(record)); // inventory.read((char *)&record, sizeof(record)); //older method of type casting } // Move to the desired record and read it into record. cout << "Which record do you want to edit?"; cin >> recNum; inventory.seekg(recNum * sizeof(record), ios::beg); inventory.read(reinterpret_cast(&record), sizeof(record)); // inventory.read((char *)&record, sizeof(record)); //older method of type casting // Get new data from user and edit in-memory record. cout << endl << "Current information" << endl; cout << "Description: "; cout << record.desc << endl; cout << "Quantity: "; cout << record.qty << endl; cout << "Price: "; cout << record.price << endl << endl; cout << "Enter the new data:\n"; cout << "Description: "; cin.ignore(); //skip next character in input buffer cin.getline(record.desc, DESC_SIZE); cout << "Quantity: "; cin >> record.qty; cout << "Price: "; cin >> record.price; // Move to the right place in file and write the record. inventory.seekp(recNum * sizeof(record), ios::beg); inventory.write(reinterpret_cast(&record), sizeof(record)); // inventory.write((char *)&record, sizeof(record)); //older method of type casting // Move to the desired record and read the update record. inventory.seekg(recNum * sizeof(record), ios::beg); inventory.read(reinterpret_cast(&record), sizeof(record)); // inventory.read((char *)&record, sizeof(record)); //older method of type casting // Get new data from user and edit in-memory record. cout << endl << "Updated information" << endl; cout << "Description: "; cout << record.desc << endl; cout << "Quantity: "; cout << record.qty << endl; cout << "Price: "; cout << record.price << endl << endl; //Close the file. inventory.close(); return 0; }