How to read data from a csv file in C++ ?

DWQA QuestionsCategory: C++How to read data from a csv file in C++ ?
Editor Staff asked 2 months ago

How to read data from a csv file in C++ ?

1 Answers
Editor Staff answered 2 months ago

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 ,