0

I have a CSV file that reads something similar to:

1,2,3,4,5
"f[0, 0] + f[0, 1]",5,10,0,2

I realized that the quotations in the CSV file are not exactly "true" to a CSV file; however, this was the format I was given. The function in quotation marks are meant to act as references to the numbers within the CSV file. For example, [0, 0] would mean 1, similar to how a 2D array works. [0, 1] would mean 2, therefore 1 + 2 = 3. I have the evaluation working just fine, I just need a way to parse these lines.

With the code I have right now, it splits the file by comma but when it gets to "f[0, 0] + f[0, 1]", it splits these up into:

"f[0
0] + f[0
 1]"

since there are commas.

This is the code I currently have to read in CSV files:

string line;
string field;

vector<vector<string>> v_array;
vector<string> v;

// fills the 2D array up
while (getline(file, line))
{
    v.clear(); // clears to add the next line
    stringstream ss(line);

    while (getline(ss, field, ',')) // this is probably the problem, but idk how to work around it
    {
        v.push_back(field);
    }

    v_array.push_back(v);
}

If I run the code I have and parse my CSV file line by line, it would get something similar to:

1
2
3
4
5

"f[1
 1] + f[1
 2]"
5
10
0
2

Basically if I get everything right, the file would look like:

1,2,3,4,5
3,5,10,0,2
  • 2
    It looks like there's really a couple of questions here - one for how to parse CSV files, and the second for evaluating expressions. For the first problem, you should find something helpful here: [How can I read and parse CSV files in C++?](https://stackoverflow.com/q/1120140/6610379). For evaluating the expression, see: [Evaluating arithmetic expressions from string in C++](https://stackoverflow.com/q/9329406/6610379). – Phil Brubaker Apr 10 '19 at 11:55
  • @PhilBrubaker Actually it's just one question - which is to parse CSV files how I wanted it. I have the evaluation down but I'm not sure how I could approach the problem I have :) – This Is It Apr 10 '19 at 13:21

0 Answers0