Then in each program, where you have the struct, you just have #include "db_struct.h"
Where you add it in the structure is entirely up to you. Immediately after desc seems logical and is consistent with your input data order. But your programs wouldn't care at all if you put the ID anywhere else in the structure. You only have to ensure that it's consistent between the programs (see the header file above).
A big question would be
Do you care about backward compatibility for existing "Inventory.dat" files?
//Program 1
// This program sets up a file of blank inventory records.
#include <iostream>
#include <fstream>
usingnamespace std;
// Constants
const size_t DESC_SIZE {31};
const size_t NUM_RECORDS {5};
const size_t ID_SIZE {5};
// Declaration of InventoryItem structure
struct InventoryItem {
char desc[DESC_SIZE] {};
char ID[ID_SIZE] {};
int qty {};
double price {};
};
int main()
{
// Create an empty Inventoryitem structure.
InventoryItem record;
// Open the file for binary output.
ofstream inventory("Inventory.dat", ios::binary);
if (inventory.is_open())
// Write the blank records.
for (size_t count = 0; count < NUM_RECORDS && inventory.write(reinterpret_cast<char*>(&record), sizeof(record)); ++count)
cout << "Now writing record " << count + 1 << '\n';
else
cout << "Cannot open file for output\n";
}
When opening a file, you should always check that it has opened OK. Also the state should be checked after a file operation to make sure the operation has completed OK.