In this article we will discuss and create a class to read data from a CSV File.
What is a CSV File ?
A csv file is a kind of flat file used to store the data. In this each row contains data separated by comma.
For example,
20,hi,99 3,4,5 a,b,c 3,4,1
Creating a Class CSVReader to read Data from CSV File
Let’s create a class CSVReader that provides API to read data from a CSV File
/* * A class to read data from a csv file. */ class CSVReader { std::string fileName; std::string delimeter; public: CSVReader(std::string filename, std::string delm = ",") : fileName(filename), delimeter(delm) { } // Function to fetch data from a CSV File std::vector<std::vector<std::string> > getData(); };
It accepts the filename as constructor argument and provides a member function that reads the content of CSV file and returns the data in a vector of vector<std::string> i.e.
// Function to fetch data from a CSV File std::vector<std::vector<std::string> > getData();
All comma separated elements in a single row will be stored in vector of strings i.e.
std::vector<std::string>
Now Data of each row i.e. vector<string> will be stored in another vector i.e.
std::vector<std::vector<std::string> >
This member function will parse the content of csv file line by line and store data in vector< vector <string> > i.e.
/* * Parses through csv file line by line and returns the data * in vector of vector of strings. */ std::vector<std::vector<std::string> > CSVReader::getData() { std::ifstream file(fileName); std::vector<std::vector<std::string> > dataList; std::string line = ""; // Iterate through each line and split the content using delimeter while (getline(file, line)) { std::vector<std::string> vec; boost::algorithm::split(vec, line, boost::is_any_of(delimeter)); dataList.push_back(vec); } // Close the File file.close(); return dataList; }
Let’s create an object of class CSVReader class and read the data from csv file i.e.
// Creating an object of CSVWriter CSVReader reader("example.csv"); // Get the data from CSV File std::vector<std::vector<std::string> > dataList = reader.getData();
Complete example is as follows,
#include <iostream> #include <fstream> #include <vector> #include <iterator> #include <string> #include <algorithm> #include <boost/algorithm/string.hpp> /* * A class to read data from a csv file. */ class CSVReader { std::string fileName; std::string delimeter; public: CSVReader(std::string filename, std::string delm = ",") : fileName(filename), delimeter(delm) { } // Function to fetch data from a CSV File std::vector<std::vector<std::string> > getData(); }; /* * Parses through csv file line by line and returns the data * in vector of vector of strings. */ std::vector<std::vector<std::string> > CSVReader::getData() { std::ifstream file(fileName); std::vector<std::vector<std::string> > dataList; std::string line = ""; // Iterate through each line and split the content using delimeter while (getline(file, line)) { std::vector<std::string> vec; boost::algorithm::split(vec, line, boost::is_any_of(delimeter)); dataList.push_back(vec); } // Close the File file.close(); return dataList; } int main() { // Creating an object of CSVWriter CSVReader reader("example.csv"); // Get the data from CSV File std::vector<std::vector<std::string> > dataList = reader.getData(); // Print the content of row by row on screen for(std::vector<std::string> vec : dataList) { for(std::string data : vec) { std::cout<<data << " , "; } std::cout<<std::endl; } return 0; }
Output:
20 , hi , 99 , 3 , 4 , 5 , a , b , c , 3 , 4 , 1 ,
To Compile the above code use following command in Linux,
g++ –std=c++11 example.cpp
Do you want to Learn Modern C++ from best?
We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.
Check Detailed Reviews of Best Modern C++ Courses
Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.