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
|
/
/ PARSINGTHEFILE. CPP MAIN
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include "CsvReader.h"
#include "Csvline.h"
#include <cstdio>
using namespace std;
int main(int argc, char* argv) {
CsvReader reader;
vector<Csvline> lines = reader.read("C:\\Users\\E1416162\\Documents\\PE-Lab UAServer NodeIDs.csv", true);
int i = 0;
for (auto& line : lines) {
cout << "Display Name: " << lines[i].values[0].c_str() << endl;
cout << " Node ID: " << lines[i].values[1].c_str() << endl;
cout << " Data Type: " << lines[i].values[2].c_str() << endl;
cout << endl;
i++;
if (i == 20)
break;
return 0;
}
// Csvline CPP MAIN
#include "Csvline.h"
using namespace std;
void Csvline::parse(std::string line, char delimiter) {
std::stringstream inLine(line);
std::string temporaryColumn = "";
while (getline(inLine, temporaryColumn, delimiter)) {
values.push_back(temporaryColumn);
}
}
// get a column value
string Csvline::getString(int column) {
return values[column];
}
double Csvline::getDouble(int column) {
return atof(values[column].c_str());
}
int Csvline::getInt(int column) {
return atof(values[column].c_str());
}
float Csvline::getFloat(int column) {
return atof(values[column].c_str());
}
// CsvReader CPP
#include "CsvReader.h"
vector<Csvline> CsvReader::read(string fileName, bool hasHeader) {
ifstream inputFile;
vector<Csvline> lines;
inputFile.open(fileName.c_str());
string line = "";
if (hasHeader)
getline(inputFile, line);
while (getline(inputFile, line)) {
Csvline csvLine;
csvLine.parse(line);
lines.push_back(csvLine);
}
int i = 0;
printf("Line %d, value_0 = %s, value_1 = %s, value_2 = %s ", i, lines[i].values[i].c_str(), lines[i].values[i + 1].c_str(), lines[i].values[i + 2].c_str());
return lines;
}
// CsvReader HEADER
#pragma once
#ifndef CSVREADER_H
#define CSVREADER_H
#include <vector>
#include <fstream>
#include <iostream>
#include "Csvline.h"
using namespace std;
// This is to read a file
class CsvReader
{
public:
CsvReader() {}
vector<Csvline>read(string fileName, bool hasHeader);
};
#endif
// Csvline HEADER
#pragma once
//defining class
#ifndef CSVLINE_H
#define CSVLINE_H
#include <string>
#include <sstream>
#include <vector>
using namespace std;
//
class Csvline
{
public:
vector<string>values;
public:
//constructer
Csvline(){}
Csvline(const Csvline& other) {
values = other.values;
}
Csvline operator=(const Csvline& other) {
values = other.values;
}
~Csvline(){}
//get a line and return nothing
void parse(std::string lines, char delimiter = ',');
// get a column value
string getString(int column);
double getDouble(int column);
int getInt(int column);
float getFloat(int column);
};
#endif
| |