1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
ifstream infile;
ofstream outfile;
//struct
struct bookType
{
string itemName;
string listingID;
string sellerSku;
double price;
int quantity;
string openDate;
string itemNote;
int itemCondition;
string productID;
string market;
};
// Function dec
int readBooks(ifstream& infile, bookType);
// Main
int main()
{
bookType goodBooks[100]; // array of books
int count = 0;
int index = 0;
infile.open("books.txt"); // open input file
if (!infile)
{
cout << "Cannot open the input file. Program terminates!"<< endl;
}
count = readBooks(infile, goodBooks[100]);
for(index = 0; index < count; index++)
{
cout << goodBooks[index].itemName << " " << goodBooks[index].listingID << " " << goodBooks[index].sellerSku <<" "<<endl;
cout << goodBooks[index].price << " " << goodBooks[index].quantity << " " << goodBooks[index].openDate << " "
<< goodBooks[index].price << " "<<endl;
cout << goodBooks[index].itemNote <<endl;
cout << goodBooks[index].itemCondition << " "<< goodBooks[index].productID << " "<< goodBooks[index].market<<endl;
}
system ("PAUSE");
return 0;
}
int readBooks(ifstream& infile, bookType goodBooks[100]) // Function to read in the external data from the file
{
char ignore;
int index = 0;
int count = 0;
while (infile)
{
getline (infile, goodBooks[index].itemName, '\t');
infile >> goodBooks[index].listingID, '\t';
infile >> goodBooks[index].sellerSku, '\t';
infile >> goodBooks[index].price, '\t';
infile >> goodBooks[index].quantity, '\t';
getline (infile, goodBooks[index].openDate, '\t');
getline (infile, goodBooks[index].itemNote, '\t');
infile >> goodBooks[index].itemCondition, '\t';
infile >> goodBooks[index].productID, '\t';
infile >> goodBooks[index].market, '\t';
infile.get(ignore);
count = index++;
}
return(count);
}
|