Skip to content

Instantly share code, notes, and snippets.

@lukasondrejka
Last active June 26, 2022 12:37
Show Gist options
  • Save lukasondrejka/9b0f12b3231126e70353de9578ecb275 to your computer and use it in GitHub Desktop.
Save lukasondrejka/9b0f12b3231126e70353de9578ecb275 to your computer and use it in GitHub Desktop.
CSVLoader – C++ class for loading CSV files
/*
*
* 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);
}
/*
*
* 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_;
};
@lukasondrejka
Copy link
Author

lukasondrejka commented Jun 6, 2022

CSVLoader – Simple C++ class for loading CSV files

Usage

Example CSV file (example.csv)

John;Smith;1999
Mark;Brown;1995
Michael;Davis;2001
Frank;Williams;1992

Usage of CSVLoader

Include

#include "csv_loader.h"

Load CSV file

CSVLoader loader = CSVLoader("example.csv", ';');

loader.open();

while (loader.nextLine())
{
    std::cout << loader[0] << " " << loader[1] << " (" << loader[2] << ")" << std::endl;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment