1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
   | #include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <fstream>
 using namespace std;
int main()
{
 
 
 
    //const string filename = "C:/Users/click/Desktop/exemple.csv";
    ifstream filename("C:/Users/click/Desktop/exemple.csv"); // open the file
    string line, field;
    vector< vector<string> > array;  // the 2D array
    vector<string> v;                // array of values for one line only
 
    if (!filename) // error if the file doesn't exist
  {
    cerr << "Can't open file " << filename << endl;
    return 1;
  }
 
    while ( getline(filename,line) )    // get next line in file
    {
        v.clear();
        stringstream ss(line);
 
        while (getline(ss,field,';'))  // break line into comma delimitted fields
        {
            v.push_back(field);  // add each field to the 1D array
        }
 
        array.push_back(v);  // add the 1D array to the 2D array
    }
 
    // print out what was read in
 
    for (size_t i=0; i<array.size(); ++i)
    {
        for (size_t j=0; j<array[i].size(); ++j)
        {
            cout << array[i][j] << " | "; // (separate fields by |)
        }
        cout << "\n";
    }
 system("pause");
    return 0;
} | 
Partager