#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_; } 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