0

1) My input: A csv (or txt) file of integers (indexes) of arbitrary (and unlimited) number of columns and rows, such as for example

0, 1
2, 3, 4

2) Desired output: Store the integer file into a data structure. I suspect the best such data structure capable of accommodating the flexible size would be a vector of vectors.

3) What I've done so far, what remains to be done: I've completed a first stage and more trivial problem where I read a formatted nx2 csv file into two arrays. This program is the following is shown below.

The question then is how do I extend this to the nxm (instead of nx2) file size and store into vector of vectors instead of two arrays?

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include<vector>
using namespace std;

int main(){
    //define variables
    string ind, p; //variables from file are here (two columns)
    vector<int>index;
    vector<float>prob;

    //number of lines
    int i=0;

    ifstream input;
    input.open("Params05.csv"); // Params is the nx2 input file

    while(input.good()){
        //ignore first line
        string line;
        getline(input, line);

        // Two getlines for two columns (sets)
        getline(input, ind, ',');
        index.push_back(stoi(ind));
        getline(input, p, '\n'); //newline
        prob.push_back(stof(p));

        i += 1; //increment total number of laws
        
    }
    input.close(); //close file

    cout<< "Number of entries: "<< i-1 << endl;

    for (int j = 0; j < i; j++){
        cout<< index[j] << "\t" << prob[j] << endl;
    }
}

Thanks in advance for any thoughts, tips and contributions. Love this forum.

PaulR
  • 3,587
  • 14
  • 24
  • related/dupe: https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c – NathanOliver Jul 28 '20 at 18:59
  • Are you saying that the 2D vector will always have two rows, or two columns? It's a bit hard to understand what you are going for. – john Jul 28 '20 at 19:02
  • You also have the problem that it's not possible to put integers and floats into the same vector (at least not without some extra work). What was your thinking on that issue? – john Jul 28 '20 at 19:03
  • So it seems I've been confused by your sample code. If I ignore that and just read the text, then you want integers only, and you want arbitrary numbers of columns and rows. Correct? – john Jul 28 '20 at 19:07
  • correct, @john. Thanks and sorry for confusion. I thought it was a requirement here to show your current effort! – Gui Larange Jul 28 '20 at 19:16

2 Answers2

0

Well in outline all you need is an extra loop and a stringstream to read integers from each line

#include <vector>
#include <string>
#include <sstream>
using namespace std;

vector<vector<int>> vec2d;
string line;
getline(file,line); // ignore the first line
while (getline(file, line)) {
    vec2d.push_back(vector<int>()); // add row
    istringstream buffer(line);
    string num;
    while (getline(buffer, num, ',')) {
        vec2d.back().push_back(stoi(num)); // add number to last row
    }
}

Hopefully OK, I haven't tested anything

john
  • 85,011
  • 4
  • 57
  • 81
0

You need another loop. You should add tests for float , but I think you can adapt this code.

Input file:

0, 1
2, 3, 4
3, 4, 8, 9

Code:

using namespace std;

int main(){
    //define variables
    string ind, p; //variables from file are here (two columns)
    vector<vector<int>> index;
    vector<float>prob;

    //number of lines
    int i=0;

    ifstream input;
    input.open("file.txt"); // Params is the nx2 input file

    while(input.good()){
        //ignore first line
        string line;
        getline(input, line);
        if (line == "")
            break;
        cout << "Readed: " << line << endl;
        std::istringstream sinput (line);
        index.push_back (vector<int>());
        while (sinput.good()) {
            getline(sinput, ind, ',');
            index[i].push_back(stoi(ind));
        }
        i += 1; //increment total number of laws
    }
    input.close(); //close file

    cout<< "Number of entries: "<< i << endl;

    for (int j = 0; j < i; j++){
        cout << j << ": ";
        for (auto v : index[j]) {
            cout << v << ", ";;
        }
        cout << endl;
    }
}

Output:

Readed: 2, 3, 4
Readed: 3, 4, 8, 9
Number of entries: 3
0: 0, 1, 
1: 2, 3, 4, 
2: 3, 4, 8, 9, 
Manuel
  • 2,526
  • 1
  • 13
  • 17