Skip to content

Instantly share code, notes, and snippets.

@fernandozamoraj
Created September 29, 2021 04:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandozamoraj/22faf8e4b2ee45cb8e0f7dc5110319e7 to your computer and use it in GitHub Desktop.
Save fernandozamoraj/22faf8e4b2ee45cb8e0f7dc5110319e7 to your computer and use it in GitHub Desktop.
Code for creating a generic Csv Parsing Solution in C++
///////////////////////////////////////////////////////////////
// CsvLine.h
#ifndef CSVLINE_H
#define CSVLINE_H
#include <string>
#include <sstream>
#include <vector>
using namespace std;
class CsvLine
{
private:
vector<string> values;
public:
CsvLine() {}
CsvLine(const CsvLine& other) {
values = other.values;
}
CsvLine operator=(const CsvLine& other) {
values = other.values;
}
~CsvLine() {}
void parse(string line, char delimiter = ',');
string getString(int column);
double getDouble(int column);
int getInt(int column);
};
#endif
/////////////////////////////////////////////////////////////
// CsvReader.h
#ifndef CSVREADER_H
#define CSVREADER_H
#include <vector>
#include <fstream>
#include <iostream>
#include "CsvLine.h"
using namespace std;
class CsvReader
{
public:
CsvReader() {}
vector<CsvLine> read(string fileName, bool hasHeader = false);
};
#endif
////////////////////////////////////////////////////////////////////
// CsvLine.cpp
#include "CsvLine.h"
void CsvLine::parse(string line, char delimiter) {
stringstream inLine(line);
string temporaryColumn = "";
while (getline(inLine, temporaryColumn, delimiter)) {
values.push_back(temporaryColumn);
}
}
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 atoi(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);
}
return lines;
}
///////////////////////////////////////////////////////////////////////
// ParsingCsvDemo.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include "CsvReader.h"
#include "CsvLine.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv) {
CsvReader reader;
vector<CsvLine> lines = reader.read("c:\\temp\\student-records.csv", true);
for (auto line : lines) {
cout << " Id: " << line.getString(0) << endl;
cout << " Last Name: " << line.getString(1) << endl;
cout << " FirstName: " << line.getString(2) << endl;
cout << " Age: " << line.getInt(3) << endl;
cout << "Phone Number: " << line.getString(4) << endl;
cout << " GPA: " << line.getDouble(5) << endl;
cout << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment