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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Item {
int partNumber;
string partName;
int partQuantity;
double price;
Item(int p, const string& pnam, int q, double pr) : partNumber(p), partName(pnam), partQuantity(q), price(pr) {}
};
bool openFileIn(fstream&, const string&);
vector<Item> getItems(fstream&);
void showItems(const vector<Item>&);
vector<string> split(const string&, char = ',');
int main()
{
fstream dataFile; // data file
if (openFileIn(dataFile, "widgets.dat"))
{
const auto items {getItems(dataFile)};
dataFile.close();
showItems(items);
} else
cout << "File open error!" << endl;
}
//***********************************************************
// Definition of function openFileIn. Accepts a reference *
// to an fstream object as an argument. The file is opened *
// for input. The function returns true upon success, false *
// upon failure. *
//***********************************************************
bool openFileIn(fstream& file, const string& name)
{
file.open(name.c_str(), ios::in);
return file.is_open();
}
/*
* getStudent data one line at a time from
* the file. Parse the line using ',' as delimiter.
* Save data read to an Item, recorded in 'vs'.
*/
vector<Item> getItems(fstream& file)
{
vector<Item> vs;
string input;
//Remove header line
getline(file, input);
// Read item data from file using ',' as a delimiter.
while (getline(file, input)) {
const auto tokens {split(input)};
const auto& nam {tokens.at(1)};
vs.emplace_back(atoi(tokens.at(0).c_str()), nam.substr(2, nam.size() - 3), atoi(tokens.at(2).c_str()), atof(tokens.at(3).c_str()));
}
return vs;
}
/*
* show all items
*/
void showItems(const vector<Item>& vs)
{
/* Display the headings for each column, with spacing and justification flags */
printf("%-12s%-30s%-15s%-8s\n", "partNumber", "partName", "partQuantity", "price");
// Read item data from file using ',' as a delimiter.
for (const auto& item : vs) {
if (item.partNumber != 0) {
printf("%-12d%-30s%-15d%-2.2f\n",
item.partNumber,
item.partName.c_str(),
item.partQuantity,
item.price
);
}
/* If the id field is zero, then we reached the last, break out of the for loop. */
else
break;
}
}
//**************************************************************
// The split function splits s into tokens, using delim as the *
// delimiter. The tokens are added to the tokens vector. *
//**************************************************************
vector<string> split(const string& s, char delim)
{
vector<string> tokens;
int tokenStart = 0, delimPosition = s.find(delim);
// While string is not at end:
while (delimPosition != string::npos)
{
// Push the token onto the tokens vector.
tokens.push_back(s.substr(tokenStart, delimPosition - tokenStart));
// Move delimPosition to the next character position.
delimPosition++;
// Move tokenStart to delmiPosition.
tokenStart = delimPosition;
// Get next delimPosition.
delimPosition = s.find(delim, tokenStart);
}
tokens.push_back(s.substr(tokenStart, string::npos));
return tokens;
}
|