1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
class csv_reader
{
private:
std::ifstream m_file;
void split(const std::string& in, char sep, std::vector<std::string>& out)
{
std::stringstream stream(in);
// check both failbit and eofbit
while (stream && !stream.eof())
{
std::string token;
std::getline(stream, token, sep);
out.push_back(token);
}
}
public:
csv_reader(const std::string& path)
: m_file(path.c_str())
{ }
bool fetch_next_line(std::vector<std::string>& line, char sep)
{
line.clear();
// check both failbit and eofbit
if (m_file && !m_file.eof())
{
std::string line_string;
std::getline(m_file, line_string);
split(line_string, sep, line);
return true;
}
return false;
}
}; |
Partager