#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct inventory {
char item[20];
int quantity;
double cost;
};
int main()
{
ofstream fout("InvDat.dat", ios::out | ios::binary);
if(!fout) {
cout << "Cannot open file.\n";
return 1;
}
inventory inv[3];
strcpy(inv[0].item,"A");
inv[0].quantity = 3;
inv[0].cost = 9.99;
strcpy(inv[1].item, "B");
inv[1].quantity = 12;
inv[1].cost = 7.85;
strcpy(inv[2].item, "C");
inv[2].quantity = 19;
inv[2].cost = 2.75;
for(int i=0; i<3; i++)
fout.write((const char *) &inv[i], sizeof(inventory));
fout.close();
if(!fout.good()) {
cout << "A file error occurred.";
return 1;
}
return 0;
}
|