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 167 168 169 170 171 172 173 174 175
|
// Program to read a CSV file containing company items into an array of structures
//
// Author:
//
// date: 10/18/20
//
//
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
#define DEBUG 1
#define MAX_ITEMS 4
struct Item {
int partNumber;
string partName;
int partQuantity;
double price;
};
bool openFileIn(fstream &, string);
int getItems(fstream &, Item *);
int showItems(Item *);
void split(const string&, char , vector<string>& );
int main()
{
fstream dataFile; // data file
Item *items = new Item[MAX_ITEMS]; // dynamically allocate student records
if (openFileIn(dataFile, "widgets.dat"))
{
// get Student data from file into array
getItems(dataFile, items);
dataFile.close();
showItems(items);
}
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;
}
/*
* getStudent data one line at a time from
* the file. Parse the line using ',' as delimiter.
* Save data read to a Item record pointed to
* by s_ptr;
*/
int getItems(fstream &file, Item *s_ptr)
{
string input; // To hold file input
int count = 0;
// Read item data from file using ',' as a delimiter.
while (count < MAX_ITEMS && 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 item record
*/
s_ptr->partNumber = atoi(tokens.at(0).c_str());
s_ptr->partName = tokens.at(1);
s_ptr->partQuantity = tokens.at(2);
s_ptr->price = atof(tokens.at(3).c_str());
count++;
s_ptr++;
cout << endl;
}
}
/*
* show all items
*/
int showItems(item *s_ptr)
{
/* 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 (int i = 0; i < MAX_ITEMS; i++) {
if (s_ptr->partNumber != 0) {
printf("%-8d%-30s%-10s%-2.2f\n",
s_ptr->partNumber,
s_ptr->partName.c_str(),
s_ptr->partQuantity.c_str(),
s_ptr->price);
s_ptr++;
}
/* If the id field is zero, then we reached the last student, 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);
}
}
}
|