Karedia Noorsil
Karedia Noorsil

Reputation: 470

how to read data from different cells of Excel (csv) file in C++..?

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    string a1[100];
    string a2[100];
    string a3[100];
    ifstream in;
    ofstream out;
    out.open("E:\\abc.csv");
    out << "dcsdc,dcdcdc,dcsdc" << endl;
    out << "dcsdc,sdcsdc,sdcdc" << endl;
    in.open("E:\\abc.csv");
    /*
        Read from different cells
    */


}

I dont know what to write to read data from three different cells and store them in a1, a2, a3.

For .csv file ' , '(comma) is the delimiter.

Upvotes: 0

Views: 3715

Answers (1)

herohuyongtao
herohuyongtao

Reputation: 50667

Try the following (borrowed from here), it will split each line to different cells you want based on ,.

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream  data("plop.csv");

    std::string line;
    while(std::getline(data,line))
    {
        std::stringstream  lineStream(line);
        std::string        cell;
        while(std::getline(lineStream,cell,','))
        {
            // You have a cell!!!!
        }
    }
 }

Also see this question: CSV parser in C++

Upvotes: 1

Related Questions