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,

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

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.

All comma separated elements in a single row will be stored in vector of strings i.e.

Now Data of each row i.e. vector<string> will be stored in another vector i.e.

This member function will parse the content of csv file line by line and store data in vector< vector <string> > i.e.

Let’s create an object of class CSVReader class and read the data from csv file i.e.

Complete example is as follows,


To Compile the above code use following command in Linux,

g++ –std=c++11 example.cpp


Click Here to Subscribe for more Articles / Tutorials like this.