Last active
June 26, 2022 12:37
-
-
Save lukasondrejka/9b0f12b3231126e70353de9578ecb275 to your computer and use it in GitHub Desktop.
CSVLoader – C++ class for loading CSV files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* Author: Lukas Ondrejka | |
* Date: 2022-06-17 | |
* | |
* https://gist.github.com/lukasondrejka/9b0f12b3231126e70353de9578ecb275 | |
* | |
*/ | |
#include "csv_loader.h" | |
CSVLoader::CSVLoader(const std::string fileName, char separator) : | |
fileName_(fileName), | |
separator_(separator), | |
file_(new std::fstream()), | |
line_(std::vector<std::string>()) | |
{ | |
} | |
CSVLoader::CSVLoader(const std::string fileName) : | |
CSVLoader(fileName, DEFAULT_SEPARATOR) | |
{ | |
} | |
CSVLoader::~CSVLoader() | |
{ | |
this->fileName_ = ""; | |
this->separator_ = DEFAULT_SEPARATOR; | |
this->close(); | |
delete this->file_; | |
this->file_ = nullptr; | |
} | |
bool CSVLoader::isOpen() | |
{ | |
return file_->is_open(); | |
} | |
void CSVLoader::open() | |
{ | |
if (this->isOpen() == false) | |
{ | |
this->file_->open(fileName_, std::ios::in); | |
} | |
} | |
void CSVLoader::close() | |
{ | |
if (this->isOpen() == true) | |
{ | |
this->file_->close(); | |
this->line_.clear(); | |
} | |
} | |
bool CSVLoader::nextLine() | |
{ | |
if (this->isOpen() == false) | |
{ | |
return false; | |
} | |
std::string s; | |
if (std::getline(*this->file_, s)) | |
{ | |
std::stringstream line(s); | |
this->line_.clear(); | |
while (std::getline(line, s, this->separator_)) | |
{ | |
this->line_.push_back(s); | |
} | |
return true; | |
} | |
else | |
{ | |
this->close(); | |
return false; | |
} | |
} | |
int CSVLoader::lineSize() | |
{ | |
return line_.size(); | |
} | |
std::string CSVLoader::at(int index) | |
{ | |
return this->line_.at(index); | |
} | |
std::string CSVLoader::operator[](int index) | |
{ | |
return this->at(index); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* Author: Lukas Ondrejka | |
* Date: 2022-06-17 | |
* | |
* https://gist.github.com/lukasondrejka/9b0f12b3231126e70353de9578ecb275 | |
* | |
*/ | |
#pragma once | |
#include <iostream> | |
#include <string> | |
#include <sstream> | |
#include <fstream> | |
#include <vector> | |
#pragma execution_character_set( "utf-8" ) | |
#define DEFAULT_SEPARATOR ';' | |
/// <summary> CSVLoader – Simple C++ class for loading CSV files </summary> | |
class CSVLoader | |
{ | |
public: | |
/// <summary> Constructor </summary> | |
/// <param name="fileName"> Name of CSV file </param> | |
/// <param name="separator"> Separating character </param> | |
CSVLoader(const std::string fileName, char separator); | |
/// <summary> Constructor </summary> | |
/// <param name="fileName"> Name of CSV file </param> | |
CSVLoader(const std::string fileName); | |
/// <summary> Destructor </summary> | |
~CSVLoader(); | |
/// <summary> Is file open </summary> | |
/// <returns> True if open, else false </returns> | |
bool isOpen(); | |
/// <summary> Open CSV file </summary> | |
void open(); | |
/// <summary> Close CSV file </summary> | |
void close(); | |
/// <summary> Go to the next row </summary> | |
/// <returns> True if passed, else false </returns> | |
bool nextLine(); | |
/// <summary> Count of items in the current row </summary> | |
/// <returns> Count of items </returns> | |
int lineSize(); | |
/// <summary> Access to the item at the index in the current row </summary> | |
/// <param name="index"> Index od item in the current row </param> | |
/// <returns> Item </returns> | |
std::string at(int index); | |
/// <summary> Access to the item at the index in the current row </summary> | |
/// <param name="index"> Index of item in the current row </param> | |
/// <returns> Item </returns> | |
std::string operator[](int index); | |
private: | |
/// <summary> Name of CSV file </summary> | |
std::string fileName_; | |
/// <summary> Separating character </summary> | |
char separator_; | |
/// <summary> Stream to load the file </summary> | |
std::fstream* file_; | |
/// <summary> Items in the current row </summary> | |
std::vector<std::string> line_; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CSVLoader – Simple C++ class for loading CSV files
Usage
Example CSV file (
example.csv
)Usage of CSVLoader
Include
Load CSV file