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
|
#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;
// Read item data from file using ',' as a delimiter.
for (string input; getline(file, input); ) {
const auto tokens {split(input)};
vs.emplace_back(atoi(tokens.at(0).c_str()), tokens.at(1), 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("%-8s%-30s%-10s%-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("%-8d%-30s%-10d%-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;
// While string is not at end:
for (
int tokenStart = 0, delimPosition = s.find(delim);
delimPosition - tokenStart != 0; // if string is not at end
delimPosition = s.find(delim, delimPosition) // get next delimPosition
) {
// 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;
}
return tokens;
}
|