0
#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.

Karedia Noorsil
  • 430
  • 3
  • 9
  • 21
  • 1
    To read data up to a delimiter, assuming this delimiter cannot be part of the content, consider [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline) – user123 Dec 31 '13 at 19:07
  • 1
    Search StackOverflow for "[c++] read csv file". There are at least 10 answers here: https://www.google.com/search?q=stackoverflow+[c%2B%2B]+read+csv+file&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a – Thomas Matthews Dec 31 '13 at 19:11
  • possible duplicate of [c++ Read from .csv file](http://stackoverflow.com/questions/16446665/c-read-from-csv-file) – Thomas Matthews Dec 31 '13 at 19:13

1 Answers1

1

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++

Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174