#pragma once #ifndef csv_reader_h #define csv_reader_h #include #include #include #include #include using std::string; using std::vector; class csvReader { public: csvReader(string); ~csvReader(); public: string file; vector> strArray; public: int rows(); void readData(); string& operator()(int i, int j) const; }; csvReader::csvReader(string file_) { file = file_; } csvReader::~csvReader() {} int csvReader::rows() { return strArray.size(); } void csvReader::readData() { std::ifstream inFile(file); string lineStr; while (std::getline(inFile, lineStr)) { std::stringstream ss(lineStr); string str; vector lineArray; while (std::getline(ss, str, ',')) lineArray.push_back(str); strArray.push_back(lineArray); } } string& csvReader::operator()(int i, int j) const { vector lineArray; int n = strArray.size(), m; if (i < 0 || i > n - 1) std::cout << "Row Index Out Of Bounds " << std::endl; lineArray = strArray.at(i); m = lineArray.size(); if (j < 0 || j > m - 1) std::cout << "Column Index Out Of Bounds " << std::endl; return lineArray.at(j); } std::ostream& operator<<(std::ostream& cout, csvReader& cR) { vector lineArray; int n = cR.strArray.size(), m; for (int i = 0; i < n; i++) { lineArray = cR.strArray.at(i); m = lineArray.size(); for (int j = 0; j < m; j++) cout << lineArray.at(j) << " "; if (i < n - 1) cout << std::endl; } return cout; } #endif