In: Computer Science
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct Product
{
string itemname;
int id;
string itemcolor;
double cost;
};
void fillProduct(Product[10], int& );//read data from a file
and store in an array
void writeProduct(Product[10], int);//write the array into a
file
void writeBinary(Product[10], int);//write the array into a file in
binary mode
void printbinary(Product[10], int);//read data from the binary file
and print
int main()
{
Product myList[10];
int numItems = 0;
fillProduct(myList, numItems);
writeProduct(myList, numItems);
writeBinary(myList, numItems);
printbinary(myList, numItems);
return 0;
}
void fillProduct(Product myList[10], int& numItems)
{
ifstream indata;
indata.open("lab6data.txt");
if (!indata)
{
cout << "Error opening file.
Program aborting.\n";
}
else
{
string tname;
getline(indata, tname);
while (!indata.eof())
{
myList[numItems].itemname = tname;
indata >>
myList[numItems].id;
indata.ignore();
getline(indata,
myList[numItems].itemcolor);
indata >>
myList[numItems].cost;
indata.ignore();
numItems++;
getline(indata,
tname);
} // END WHILE
}
indata.close();
}
void writeProduct(Product myList[10], int numItems)
{
ofstream outdata;
outdata.open("output.txt");
if (!outdata)
{
cout << "Error opening file.
Program aborting.\n";
}
else
{
outdata << left;
outdata << setw(15) <<
"Item Name"
<<
setw(15) << "ID"
<<
setw(15) << "Color"
<<
setw(15) << "Cost" << endl;
outdata << setw(15) <<
"---------"
<<
setw(15) << "--"
<<
setw(15) << "-----"
<<
setw(15) << "----" << endl;
for (int k = 0; k < numItems;
k++)
{
outdata <<
setw(15) << myList[k].itemname
<< setw(15) <<
myList[k].id
<< setw(15) <<
myList[k].itemcolor
<< setw(15) <<
myList[k].cost << endl;
}
}
outdata.close();
}
void writeBinary(Product myList[10], int numItems)
{
ofstream outbinary;
outbinary.open("binary.dat", ios::binary);
if (!outbinary)
{
cout << "Error opening file.
Program aborting.\n";
}
else
{
for (int k = 0; k < numItems;
k++)
{
//TODO Write a
single statement below that writes data in myList[k] into
binary.dat
}
}
outbinary.close();
}
void printbinary(Product myList[10],int numItems) {
ifstream inbinary;
inbinary.open("binary.dat", ios::binary);
if (!inbinary)
{
cout << "Error opening file.
Program aborting.\n";
}
else
{
cout << left;
cout<< setw(15) <<
"Item Name"
<<
setw(15) << "ID"
<<
setw(15) << "Color"
<<
setw(15) << "Cost" << endl;
cout << setw(15) <<
"---------"
<<
setw(15) << "--"
<<
setw(15) << "-----"
<<
setw(15) << "----" << endl;
for (int k = 0; k < numItems;
k++)
{
//TODO Write a
single statement below that read data from binary.dat and store
them in myList[k].
//TODO Print
the data stored in myList[k], and the output should have the same
format as the example in the instruction.
}
}
inbinary.close();
}
C++ code screenshot:
C++ code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct Product
{
string itemname;
int id;
string itemcolor;
double cost;
};
void fillProduct(Product[10], int &); //read data from a file and store in an array
void writeProduct(Product[10], int); //write the array into a file
void writeBinary(Product[10], int); //write the array into a file in binary mode
void printbinary(Product[10], int); //read data from the binary file and print
int main()
{
Product myList[10];
int numItems = 0;
fillProduct(myList, numItems);
writeProduct(myList, numItems);
writeBinary(myList, numItems);
printbinary(myList, numItems);
return 0;
}
void fillProduct(Product myList[10], int &numItems)
{
ifstream indata;
indata.open("lab6data.txt");
if (!indata)
{
cout << "Error opening file. Program aborting.\n";
}
else
{
string tname;
getline(indata, tname);
while (!indata.eof())
{
myList[numItems].itemname = tname;
indata >> myList[numItems].id;
indata.ignore();
getline(indata, myList[numItems].itemcolor);
indata >> myList[numItems].cost;
indata.ignore();
numItems++;
getline(indata, tname);
} // END WHILE
}
indata.close();
}
void writeProduct(Product myList[10], int numItems)
{
ofstream outdata;
outdata.open("output.txt");
if (!outdata)
{
cout << "Error opening file. Program aborting.\n";
}
else
{
outdata << left;
outdata << setw(15) << "Item Name"
<< setw(15) << "ID"
<< setw(15) << "Color"
<< setw(15) << "Cost" << endl;
outdata << setw(15) << "---------"
<< setw(15) << "--"
<< setw(15) << "-----"
<< setw(15) << "----" << endl;
for (int k = 0; k < numItems; k++)
{
outdata << setw(15) << myList[k].itemname
<< setw(15) << myList[k].id
<< setw(15) << myList[k].itemcolor
<< setw(15) << myList[k].cost << endl;
}
}
outdata.close();
}
void writeBinary(Product myList[10], int numItems)
{
ofstream outbinary;
outbinary.open("binary.dat", ios::binary);
if (!outbinary)
{
cout << "Error opening file. Program aborting.\n";
}
else
{
for (int k = 0; k < numItems; k++)
{
//TODO Write a single statement below that writes data in myList[k] into binary.dat
outbinary.write((char *)&myList[k], sizeof(Product));
}
}
outbinary.close();
}
void printbinary(Product myList[10], int numItems)
{
ifstream inbinary;
inbinary.open("binary.dat", ios::binary);
if (!inbinary)
{
cout << "Error opening file. Program aborting.\n";
}
else
{
cout << left;
cout << setw(15) << "Item Name"
<< setw(15) << "ID"
<< setw(15) << "Color"
<< setw(15) << "Cost" << endl;
cout << setw(15) << "---------"
<< setw(15) << "--"
<< setw(15) << "-----"
<< setw(15) << "----" << endl;
for (int k = 0; k < numItems; k++)
{
//TODO Write a single statement below that read data from binary.dat and store them in myList[k].
inbinary.read((char *)&myList[k], sizeof(Product));
//TODO Print the data stored in myList[k], and the output should have the same format as the example in the instruction.
cout << setw(15) << myList[k].itemname
<< setw(15) << myList[k].id
<< setw(15) << myList[k].itemcolor
<< setw(15) << myList[k].cost << endl;
}
}
inbinary.close();
}
Output: