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
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
#include <sstream>
using namespace std;
struct Entry {
string eb, ed, ee, ef, eh, ei, ej, el, ek, em, en, er, es, et, eu, ev, ew, ex, ey, ez;
int ea, eg, ec, eo, ep, eq;
friend ostream& operator<<(ostream& os, const Entry e);
friend istream& operator>>(istream& is, Entry& e);
};
Entry parse_Line(ifstream &source);
bool read_File(const char*);
void write_File(vector <Entry>& data);
//overloading operator << and >> to be able to print out the information needed.
ostream& operator<<(ostream& os, const Entry e)
{
os << "d: " << e.ed << " e: " << e.ee << " f: " << e.ef << " h: " << e.ei << " m: " << e.em << "\n";
return os;
}
istream& operator>>(istream& is, Entry& e){
getline(e.ea, ',');
getline(is >> ws, e.eb, ',');
getline(is >> ws, e.ec, ',');
getline(is >> ws, e.ed, ',');
getline(is >> ws, e.ee, ',');
getline(is >> ws, e.ef, ',');
getline(is >> ws, e.eg, ',');
getline(is >> ws, e.eh, ',');
getline(is >> ws, e.ei, ',');
getline(is >> ws, e.ej, ',');
getline(is >> ws, e.ek, ',');
getline(is >> ws, e.el, ',');
getline(is >> ws, e.em, ',');
getline(is >> ws, e.en, ',');
getline(is >> ws, e.eo, ',');
getline(is >> ws, e.ep, ',');
getline(is >> ws, e.eq, ',');
getline(is >> ws, e.er, ',');
getline(is >> ws, e.es, ',');
getline(is >> ws, e.et, ',');
getline(is >> ws, e.eu, ',');
getline(is >> ws, e.ev, ',');
getline(is >> ws, e.ew, ',');
getline(is >> ws, e.ex, ',');
getline(is >> ws, e.ey, ',');
return(is >> e.ez);
}
bool read_File(const char* fileName, vector <Entry>& allData){
string line;
ifstream fileInput;
fileInput.open(fileName, ios::in);
if (fileInput.is_open()){
// take each line, put it into the parse_Line function, then put it into the allData vector.
for (Entry e; fileInput >> e; allData.push_back(move(e)));
fileInput.close();
//cout << allData[0];
write_File(allData);
return true;
} else {
return false;
}
}
void write_File(vector <Entry>& data){
for (int i=0; i<=data.size(); i++ ){
cout << data[i] << " ";
}
return;
}
int main (int argc, char* argv[]) {
//check for file
if (argc < 2){
return(cout << "No file name specified\n"),1;
}
//read in file name to a function using following:
string str(argv[1]);
vector <Entry> data;
if (!read_File(argv[1], data)){
return(cout << "That file name is invalid\n"), 2;
}
const char* nameStr = str.c_str();
read_File(nameStr, data);
return 0;
}
|