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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
#define DEBUG 1
#define MAX_WIDGETS 20
struct Widget {
int number;
string description;
int quantity;
double price;
};
bool openFileIn(fstream &, string);
int getWidgets(fstream &, Widget *);
int showWidgets(Widget *);
void split(const string&, char , vector<string>& );
int main()
{
fstream dataFile; // data file
Widget *widgets = new Widget[MAX_WIDGETS]; // dynamically allocate widget records
if (openFileIn(dataFile, "widgets.dat"))
{
// get item data from file into array
getWidgets(dataFile, widgets);
dataFile.close();
showWidgets(widgets);
}
else
cout << "File open error!" << endl;
return 0;
}
//***********************************************************
// 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, string name)
{
file.open(name.c_str(), ios::in);
if (file.fail())
return false;
else
return true;
}
/*
* getWidget data one line at a time from
* the file. Parse the line using ',' as delimiter.
* Save data read to a Widget record pointed to
* by s_ptr;
*/
int getWidgets(fstream &file, Widget *s_ptr)
{
string input; // To hold file input
int count = 0;
// Read student data from file using ',' as a delimiter.
while (count < MAX_WIDGETS && getline(file, input)) {
// vector to hold the tokens.
vector<string> tokens;
// Tokenize str1, using ' ' as the delimiter.
split(input, ',', tokens);
/*
* copy the tokens to the widget record
*/
s_ptr->number = atoi(tokens.at(0).c_str());
s_ptr->description = tokens.at(1);
s_ptr->quantity = atoi(tokens.at(2).c_str());
s_ptr->price = atof(tokens.at(3).c_str());
count++;
cout << endl;
}
}
/*
* show all widgets
*/
int showWidgets(Widget *s_ptr)
{
/* Display the headings for each column, with spacing and justification flags */
printf("%-20s%-30s%-25s%-8s\n", "Part Number", "Description", "Quantity in Stock", "Price");
// Read widget data from file using ',' as a delimiter.
for (int i = 0; i < MAX_WIDGETS; i++) {
if (s_ptr->number != 0) {
printf("%-20d%-30s%-25s%-2.2f\n",
s_ptr->number,
s_ptr->description.c_str(),
s_ptr->quantity,
s_ptr->price);
s_ptr++;
}
/* If the number field is zero, then we reached the last item description., 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. *
//**************************************************************
void split(const string& s, char delim, vector<string>& tokens)
{
int tokenStart = 0; // Starting position of the next token
// Find the first occurrence of the delimiter.
int delimPosition = s.find(delim);
// While we haven't run out of delimiters...
while (delimPosition != string::npos)
{
// Extract the token.
string tok = s.substr(tokenStart, delimPosition - tokenStart);
// Push the token onto the tokens vector.
tokens.push_back(tok);
// Move delimPosition to the next character position.
delimPosition++;
// Move tokenStart to delmiPosition.
tokenStart = delimPosition;
// Find the next occurrence of the delimiter.
delimPosition = s.find(delim, delimPosition);
// If no more delimiters, extract the last token.
if (delimPosition == string::npos)
{
// Extract the token.
string tok = s.substr(tokenStart, delimPosition - tokenStart);
// Push the token onto the vector.
tokens.push_back(tok);
}
}
}
|